#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.RenderingTasks { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Rendering")] [Opsive.Shared.Utility.Description("Sets material texture with optional tiling and offset.")] public class SetMaterialTexture : TargetGameObjectAction { [Tooltip("The shader property name for the texture (default: _MainTex).")] [SerializeField] protected SharedVariable m_PropertyName = "_MainTex"; [Tooltip("The texture to set.")] [SerializeField] protected SharedVariable m_Texture; [Tooltip("The texture tiling (x, y).")] [SerializeField] protected SharedVariable m_Tiling = Vector2.one; [Tooltip("The texture offset (x, y).")] [SerializeField] protected SharedVariable m_Offset = Vector2.zero; private Renderer m_ResolvedRenderer; private Material m_Material; /// /// Called when the action starts. /// public override void OnStart() { base.OnStart(); m_ResolvedRenderer = m_ResolvedGameObject.GetComponent(); if (m_ResolvedRenderer != null) { m_Material = m_ResolvedRenderer.material; } } /// /// Updates the texture settings. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedRenderer == null) { return TaskStatus.Success; } if (m_Material == null) { m_Material = m_ResolvedRenderer.material; } if (m_Material == null) { return TaskStatus.Success; } var propertyName = string.IsNullOrEmpty(m_PropertyName.Value) ? "_MainTex" : m_PropertyName.Value; if (!m_Material.HasProperty(propertyName)) { return TaskStatus.Success; } if (m_Texture.Value != null) { m_Material.SetTexture(propertyName, m_Texture.Value); } m_Material.SetTextureScale(propertyName, m_Tiling.Value); m_Material.SetTextureOffset(propertyName, m_Offset.Value); return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_PropertyName = "_MainTex"; m_Texture = null; m_Tiling = Vector2.one; m_Offset = Vector2.zero; } } } #endif