Game Object Material#
Game Object / Game Object Material
Can Read
Cannot Write
Description#
The Material used by the Renderer component of a Game Object
Note
Returns the shared Material asset, not the instance the Renderer may have created
Parameters#
| Name | Description |
|---|---|
| Game Object | The Game Object the Material is read from |
| Index | Which of the Materials of the Renderer is returned |
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Game Object Material")]
[Category("Game Object/Game Object Material")]
[Description("The Material used by the Renderer component of a Game Object")]
[Image(typeof(IconMaterial), ColorTheme.Type.Blue, typeof(BadgeCubeOutline))]
[Parameter("Game Object", "The Game Object the Material is read from")]
[Parameter("Index", "Which of the Materials of the Renderer is returned")]
[Note("Returns the shared Material asset, not the instance the Renderer may have created")]
[Keywords("Renderer", "Mesh", "Shared", "Shader", "Component", "Read")]
[Serializable]
public class TypeMaterialGameObject : TypeMaterial
{
[SerializeField] private GetGameObject m_GameObject = new GetGameObject();
[SerializeField] private GetInteger m_Index = new GetInteger(0);
public override Material Get(Args args)
{
GameObject gameObject = this.m_GameObject.Get(args);
if (gameObject == null) return null;
if (!gameObject.TryGetComponent(out Renderer renderer)) return null;
int index = this.m_Index.GetInt(args);
if (index < 0) return null;
if (index == 0) return renderer.sharedMaterial;
List<Material> materials = ListPool<Material>.Get();
try
{
renderer.GetSharedMaterials(materials);
Material result = index < materials.Count ? materials[index] : null;
return result;
}
finally
{
ListPool<Material>.Release(materials);
}
}
public override string ToString()
{
return $"{this.m_GameObject} Material";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}