playMaker

Author Topic: GetMaterialTexture (beta 9 user)  (Read 1234 times)

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • View Profile
    • blog
GetMaterialTexture (beta 9 user)
« on: July 04, 2011, 08:54:21 PM »
as the name let suppose ^^

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.Material)]
[Tooltip("Get a texture from a material on a GameObject")]
public class GetMaterialTexture : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Renderer))]
public FsmOwnerDefault gameObject;
public FsmInt materialIndex;
[UIHint(UIHint.NamedTexture)]
public FsmString namedTexture;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmTexture storedTexture;
public bool getFromSharedMaterial;

public override void Reset()
{
gameObject = null;
materialIndex = 0;
namedTexture = "_MainTex";
storedTexture = null;
getFromSharedMaterial = false;
}

public override void OnEnter ()
{
DoGetMaterialTexture();
Finish();
}

void DoGetMaterialTexture()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
return;

if (go.renderer == null)
{
LogError("Missing Renderer!");
return;
}

string namedTex = namedTexture.Value;
if (namedTex == "")
namedTex = "_MainTex";

if (materialIndex.Value == 0 && !getFromSharedMaterial)
storedTexture.Value = go.renderer.material.GetTexture(namedTex);

else if(materialIndex.Value == 0 && getFromSharedMaterial)
storedTexture.Value = go.renderer.sharedMaterial.GetTexture(namedTex);

else if (go.renderer.materials.Length > materialIndex.Value && !getFromSharedMaterial)
{
var materials = go.renderer.materials;
storedTexture.Value = go.renderer.materials[materialIndex.Value].GetTexture(namedTex);
go.renderer.materials = materials;
}

else if (go.renderer.materials.Length > materialIndex.Value && getFromSharedMaterial)
{
var materials = go.renderer.sharedMaterials;
storedTexture.Value = go.renderer.sharedMaterials[materialIndex.Value].GetTexture(namedTex);
go.renderer.materials = materials;
}
}
}


same story with the shared material, well in case you modified a mat on an object , which will get you an instance of it , so either grab it instance or it original mat(if i not confuse myself here XD).