Search Unity

How do I change this from a random colour to a constant?

Discussion in 'Scripting' started by davejones1, Mar 21, 2018.

  1. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HighlighterSpectrum : HighlighterInteractive
    5. {
    6.     public bool random = true;
    7.     public float velocity = 0.13f;
    8.  
    9.     private float t;
    10.  
    11.     #region MonoBehaviour
    12.     //
    13.     protected override void Awake()
    14.     {
    15.         base.Awake();
    16.  
    17.         t = random ? Random.value : 0f;
    18.     }
    19.  
    20.     //
    21.     protected override void Update()
    22.     {
    23.         base.Update();
    24.         h.ConstantOnImmediate(ColorTool.GetColor(t));
    25.         t += Time.deltaTime * velocity;
    26.         t %= 1f;
    27.     }
    28.     #endregion
    29. }
    30.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Colors can't be declared as constants. You could, however, use readonly to prevent it from being changed:

    Code (CSharp):
    1. private readonly Color _myColor = new Color(1, 2, 3);
    Any attempt to set _myColor to something else will result in a compiler error.
     
    davejones1 likes this.
  3. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Where in the script would I add this?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Anywhere within the main body of the class. For example, directly below your `private float t;` declaration.
     
    davejones1 likes this.
  5. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    OK ill try this.
     
  6. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HighlighterSpectrum : HighlighterInteractive
    5. {
    6.     public bool random = true;
    7.     public float velocity = 0.13f;
    8.  
    9.     private float t;
    10.  
    11.     private readonly Color _myColor = new Color (22, 34, 23);
    12.  
    13.  
    14.  
    15.     #region MonoBehaviour
    16.     //
    17.     protected override void Awake()
    18.     {
    19.         base.Awake();
    20.  
    21.         t = random ? Random.value : 0f;
    22.     }
    23.  
    24.     //
    25.     protected override void Update()
    26.     {
    27.         base.Update();
    28.         h.ConstantOnImmediate(ColorTool.GetColor(t));
    29.         t += Time.deltaTime * velocity;
    30.         t %= 1f;
    31.     }
    32.     #endregion
    33. }
    34.  
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Well, you need to use the new _myColor property you'd just added. It looks like you'd replace `ColorTool.GetColor(t)` with `_myColor`, but I don't know what "h.ConstantOnImmediate" is.
     
    davejones1 likes this.
  8. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HighlighterSpectrum : HighlighterInteractive
    5. {
    6.     public bool random = true;
    7.     public float velocity = 0.13f;
    8.  
    9.     private float t;
    10.  
    11.     private readonly Color _myColor = new Color (22, 34, 23);
    12.  
    13.  
    14.  
    15.     #region MonoBehaviour
    16.     //
    17.     protected override void Awake()
    18.     {
    19.         base.Awake();
    20.  
    21.         t = random ? Random.value : 0f;
    22.     }
    23.  
    24.     //
    25.     protected override void Update()
    26.     {
    27.         base.Update();
    28.         h.ConstantOnImmediate(_myColor);
    29.         t += Time.deltaTime * velocity;
    30.         t %= 1f;
    31.  
    32.    
    33.     }
    34.     #endregion
    35. }
    36.  
     
  9. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Does `h.ConstantOnImmediate` change the color? If so, just pass a different color at whatever rate you want the change to occur. Looks like you're trying to use `t` to control this mechanism. If so, something like this probably works:

    Code (CSharp):
    1.  
    2. t += Time.deltaTime * velocity;
    3. h.ConstantOnImmediate(t % 1f > 0.5f ? _myColor : Color.black);
    4.  
    That should alternate between the _myColor value and blank. But I'm just assuming that h.ConstantOnImmediate can actually be used to change the color of whatever this thing is at runtime. (I have no idea what that class represents or how it works.)
     
  10. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    The class that ConstantOnImmediate is from can be seen below. This is the first half of the partial class.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. namespace HighlightingSystem
    6. {
    7.     public partial class Highlighter : MonoBehaviour
    8.     {
    9.         #region Editable Parameters
    10.         // Only these types of Renderers will be highlighted
    11.         static public readonly List<System.Type> types = new List<System.Type>()
    12.         {
    13.         typeof(MeshRenderer),
    14.         typeof(SkinnedMeshRenderer),
    15.         typeof(SpriteRenderer),
    16.         typeof(ParticleSystemRenderer),
    17.         };
    18.         #endregion
    19.         #region Public Methods
    20.         /// <summary>
    21.         /// Renderers reinitialization.
    22.         /// Call this method if your highlighted object has changed it's materials, renderers or child objects.
    23.         /// Can be called multiple times per update - renderers reinitialization will occur only once.
    24.         /// </summary>
    25.         public void ReinitMaterials()
    26.         {
    27.             renderersDirty = true;
    28.         }
    29.         /// <summary>
    30.         /// Set color for one-frame highlighting mode.
    31.         /// </summary>
    32.         /// <param name='color'>
    33.         /// Highlighting color.
    34.         /// </param>
    35.         public void OnParams(Color color)
    36.         {
    37.             onceColor = color;
    38.         }
    39.         /// <summary>
    40.         /// Turn on one-frame highlighting.
    41.         /// </summary>
    42.         public void On()
    43.         {
    44.             once = true;
    45.         }
    46.         /// <summary>
    47.         /// Turn on one-frame highlighting with specified color.
    48.         /// Can be called multiple times per update, color only from the latest call will be used.
    49.         /// </summary>
    50.         /// <param name='color'>
    51.         /// Highlighting color.
    52.         /// </param>
    53.         public void On(Color color)
    54.         {
    55.             onceColor = color;
    56.             once = true;
    57.         }
    58.         /// <summary>
    59.         /// Set flashing parameters.
    60.         /// </summary>
    61.         /// <param name='color1'>
    62.         /// Starting color.
    63.         /// </param>
    64.         /// <param name='color2'>
    65.         /// Ending color.
    66.         /// </param>
    67.         /// <param name='freq'>
    68.         /// Flashing frequency (times per second).
    69.         /// </param>
    70.         public void FlashingParams(Color color1, Color color2, float freq)
    71.         {
    72.             flashingColorMin = color1;
    73.             flashingColorMax = color2;
    74.             flashingFreq = freq;
    75.         }
    76.         /// <summary>
    77.         /// Turn on flashing.
    78.         /// </summary>
    79.         public void FlashingOn()
    80.         {
    81.             flashing = true;
    82.         }
    83.         /// <summary>
    84.         /// Turn on flashing from color1 to color2.
    85.         /// </summary>
    86.         /// <param name='color1'>
    87.         /// Starting color.
    88.         /// </param>
    89.         /// <param name='color2'>
    90.         /// Ending color.
    91.         /// </param>
    92.         public void FlashingOn(Color color1, Color color2)
    93.         {
    94.             flashingColorMin = color1;
    95.             flashingColorMax = color2;
    96.             flashing = true;
    97.         }
    98.         /// <summary>
    99.         /// Turn on flashing from color1 to color2 and specified frequency.
    100.         /// </summary>
    101.         /// <param name='color1'>
    102.         /// Starting color.
    103.         /// </param>
    104.         /// <param name='color2'>
    105.         /// Ending color.
    106.         /// </param>
    107.         /// <param name='freq'>
    108.         /// Flashing frequency (times per second).
    109.         /// </param>
    110.         public void FlashingOn(Color color1, Color color2, float freq)
    111.         {
    112.             flashingColorMin = color1;
    113.             flashingColorMax = color2;
    114.             flashingFreq = freq;
    115.             flashing = true;
    116.         }
    117.         /// <summary>
    118.         /// Turn on flashing with specified frequency.
    119.         /// </summary>
    120.         /// <param name='f'>
    121.         /// Flashing frequency (times per second).
    122.         /// </param>
    123.         public void FlashingOn(float freq)
    124.         {
    125.             flashingFreq = freq;
    126.             flashing = true;
    127.         }
    128.         /// <summary>
    129.         /// Turn off flashing.
    130.         /// </summary>
    131.         public void FlashingOff()
    132.         {
    133.             flashing = false;
    134.         }
    135.         /// <summary>
    136.         /// Switch flashing mode.
    137.         /// </summary>
    138.         public void FlashingSwitch()
    139.         {
    140.             flashing = !flashing;
    141.         }
    142.         /// <summary>
    143.         /// Set constant highlighting color.
    144.         /// </summary>
    145.         /// <param name='color'>
    146.         /// Constant highlighting color.
    147.         /// </param>
    148.         public void ConstantParams(Color color)
    149.         {
    150.             constantColor = color;
    151.         }
    152.         /// <summary>
    153.         /// Fade in constant highlighting using specified transition duration.
    154.         /// </summary>
    155.         /// <param name="time">
    156.         /// Transition time.
    157.         /// </param>
    158.         public void ConstantOn(float time = 0.25f)
    159.         {
    160.             transitionTime = (time >= 0f ? time : 0f);
    161.             transitionTarget = 1f;
    162.         }
    163.         /// <summary>
    164.         /// Fade in constant highlighting using specified color and transition duration.
    165.         /// </summary>
    166.         /// <param name="color">
    167.         /// Constant highlighting color.
    168.         /// </param>
    169.         /// <param name="time">
    170.         /// Transition duration.
    171.         /// </param>
    172.         public void ConstantOn(Color color, float time = 0.25f)
    173.         {
    174.             constantColor = color;
    175.             transitionTime = (time >= 0f ? time : 0f);
    176.             transitionTarget = 1f;
    177.         }
    178.         /// <summary>
    179.         /// Fade out constant highlighting using specified transition duration.
    180.         /// </summary>
    181.         /// <param name="time">
    182.         /// Transition time.
    183.         /// </param>
    184.         public void ConstantOff(float time = 0.25f)
    185.         {
    186.             transitionTime = (time >= 0f ? time : 0f);
    187.             transitionTarget = 0f;
    188.         }
    189.         /// <summary>
    190.         /// Switch constant highlighting using specified transition duration.
    191.         /// </summary>
    192.         /// <param name="time">
    193.         /// Transition time.
    194.         /// </param>
    195.         public void ConstantSwitch(float time = 0.25f)
    196.         {
    197.             transitionTime = (time >= 0f ? time : 0f);
    198.             transitionTarget = (transitionTarget > 0f ? 0f : 1f);
    199.         }
    200.         /// <summary>
    201.         /// Turn on constant highlighting immediately (without fading in).
    202.         /// </summary>
    203.         public void ConstantOnImmediate()
    204.         {
    205.             transitionValue = transitionTarget = 1f;
    206.         }
    207.         /// <summary>
    208.         /// Turn on constant highlighting using specified color immediately (without fading in).
    209.         /// </summary>
    210.         /// <param name='color'>
    211.         /// Constant highlighting color.
    212.         /// </param>
    213.         public void ConstantOnImmediate(Color color)
    214.         {
    215.             constantColor = color;
    216.             transitionValue = transitionTarget = 1f;
    217.         }
    218.         /// <summary>
    219.         /// Turn off constant highlighting immediately (without fading out).
    220.         /// </summary>
    221.         public void ConstantOffImmediate()
    222.         {
    223.             transitionValue = transitionTarget = 0f;
    224.         }
    225.         /// <summary>
    226.         /// Switch constant highlighting immediately (without fading in/out).
    227.         /// </summary>
    228.         public void ConstantSwitchImmediate()
    229.         {
    230.             transitionValue = transitionTarget = (transitionTarget > 0f ? 0f : 1f);
    231.         }
    232.         /// <summary>
    233.         /// Turn off all types of highlighting (occlusion mode remains intact).
    234.         /// </summary>
    235.         public void Off()
    236.         {
    237.             once = false;
    238.             flashing = false;
    239.             transitionValue = transitionTarget = 0f;
    240.         }
    241.         /// <summary>
    242.         /// Destroy this Highlighter component.
    243.         /// </summary>
    244.         ///
    245.         public void Die()
    246.         {
    247.             Destroy(this);
    248.         }
    249.         #endregion
    250.         #region Deprecated Methods
    251.         /// <summary>
    252.         /// DEPRECATED. Use seeThrough property directly. Set see-through mode
    253.         /// </summary>
    254.         public void SeeThrough(bool state)
    255.         {
    256.             seeThrough = state;
    257.         }
    258.         /// <summary>
    259.         /// DEPRECATED. Use seeThrough property directly. Enable see-through mode
    260.         /// </summary>
    261.         public void SeeThroughOn()
    262.         {
    263.             seeThrough = true;
    264.         }
    265.         /// <summary>
    266.         /// DEPRECATED. Use seeThrough property directly. Disable see-through mode
    267.         /// </summary>
    268.         public void SeeThroughOff()
    269.         {
    270.             seeThrough = false;
    271.         }
    272.         /// <summary>
    273.         /// DEPRECATED. Use seeThrough property directly. Switch see-through mode
    274.         /// </summary>
    275.         public void SeeThroughSwitch()
    276.         {
    277.             seeThrough = !seeThrough;
    278.         }
    279.         /// <summary>
    280.         /// DEPRECATED. Use occluder property directly. Enable occluder mode. Non-see-through occluders will be used only in case frame depth buffer is not accessible.
    281.         /// </summary>
    282.         public void OccluderOn()
    283.         {
    284.             occluder = true;
    285.         }
    286.         /// <summary>
    287.         /// DEPRECATED. Use occluder property directly. Disable occluder mode. Non-see-through occluders will be used only in case frame depth buffer is not accessible.
    288.         /// </summary>
    289.         public void OccluderOff()
    290.         {
    291.             occluder = false;
    292.         }
    293.         /// <summary>
    294.         /// DEPRECATED. Use occluder property directly. Switch occluder mode. Non-see-through occluders will be used only in case frame depth buffer is not accessible.
    295.         /// </summary>
    296.         public void OccluderSwitch()
    297.         {
    298.             occluder = !occluder;
    299.         }
    300.         #endregion
    301.     }
    302. }
    303.  
     
  11. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183

    Second half of the partial class is added below.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Rendering;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. namespace HighlightingSystem
    6. {
    7.     [DisallowMultipleComponent]
    8.     public partial class Highlighter : MonoBehaviour
    9.     {
    10.         private enum Mode : int
    11.         {
    12.             None,
    13.             Highlighter,
    14.             HighlighterSeeThrough,
    15.             OccluderSeeThrough,
    16.         }
    17.         // Constants (don't touch this!)
    18.         #region Constants
    19.         //
    20.         public const string keywordSeeThrough = "SEE_THROUGH";
    21.         // 2 * PI constant for sine flashing
    22.         private const float doublePI = 2f * Mathf.PI;
    23.         // Occlusion color
    24.         private readonly Color occluderColor = new Color(0f, 0f, 0f, 0f);
    25.         // Highlighting modes rendered in that order
    26.         static private readonly Mode[] renderingOrder = new Mode[] { Mode.Highlighter, Mode.HighlighterSeeThrough, Mode.OccluderSeeThrough };
    27.         #endregion
    28.         #region Static Fields
    29.         // List of all highlighters in scene
    30.         static private HashSet<Highlighter> highlighters = new HashSet<Highlighter>();
    31.         // Shader property ID cached constants
    32.         static private ShaderPropertyID shaderPropertyID
    33.         {
    34.             get { return HighlightingBase.shaderPropertyID; }
    35.         }
    36.         #endregion
    37.         #region Public Fields
    38.         /// <summary>
    39.         /// Controls see-through mode for highlighters or occluders. When set to true - highlighter in this mode will not be occluded by anything (except for see-through occluders). Occluder in this mode will overlap any highlighting.
    40.         /// </summary>
    41.         [HideInInspector]
    42.         public bool seeThrough;
    43.         /// <summary>
    44.         /// Controls occluder mode. Note that non-see-through highlighting occluders will be enabled only when frame depth buffer is not available!
    45.         /// </summary>
    46.         [HideInInspector]
    47.         public bool occluder;
    48.         /// <summary>
    49.         /// Force-render highlighting of this Highlighter. No culling is performed in this case (neither frustum nor occlusion culling) and renderers from all LOD levels will be always rendered.
    50.         /// Please be considerate in enabling this mode, or you may experience performance degradation.
    51.         /// </summary>
    52.         [HideInInspector]
    53.         public bool forceRender = false;
    54.         #endregion
    55.         #region Private Fields
    56.         // Cached transform component reference
    57.         private Transform tr;
    58.         // Cached Renderers
    59.         private List<HighlighterRenderer> highlightableRenderers = new List<HighlighterRenderer>();
    60.         // Renderers reinitialization is required flag
    61.         private bool renderersDirty;
    62.         // Static list to prevent unnecessary memory allocations when grabbing renderer components
    63.         static private List<Component> sComponents = new List<Component>(4);
    64.         // Highlighting mode
    65.         private Mode mode;
    66.      
    67.         // Cached seeThrough value
    68.         private bool cachedSeeThrough;
    69.         // One-frame highlighting flag
    70.         private int cachedOnce = -1;
    71.         private bool once
    72.         {
    73.             get { return cachedOnce == Time.frameCount; }
    74.             set { cachedOnce = value ? Time.frameCount : -1; }
    75.         }
    76.         // Flashing enabled flag
    77.         private bool flashing;
    78.         // Current highlighting color
    79.         private Color currentColor;
    80.      
    81.         // Current transition value
    82.         private float transitionValue;
    83.         // Current Transition target
    84.         private float transitionTarget;
    85.         // Transition duration
    86.         private float transitionTime;
    87.         // One-frame highlighting color
    88.         private Color onceColor;
    89.      
    90.         // Flashing frequency (times per second)
    91.         private float flashingFreq;
    92.      
    93.         // Flashing from color
    94.         private Color flashingColorMin;
    95.      
    96.         // Flashing to color
    97.         private Color flashingColorMax;
    98.         // Constant highlighting color
    99.         private Color constantColor;
    100.         // Opaque shader cached reference
    101.         static private Shader _opaqueShader;
    102.         static public Shader opaqueShader
    103.         {
    104.             get
    105.             {
    106.                 if (_opaqueShader == null)
    107.                 {
    108.                     _opaqueShader = Shader.Find("Hidden/Highlighted/Opaque");
    109.                 }
    110.                 return _opaqueShader;
    111.             }
    112.         }
    113.      
    114.         // Transparent shader cached reference
    115.         static private Shader _transparentShader;
    116.         static public Shader transparentShader
    117.         {
    118.             get
    119.             {
    120.                 if (_transparentShader == null)
    121.                 {
    122.                     _transparentShader = Shader.Find("Hidden/Highlighted/Transparent");
    123.                 }
    124.                 return _transparentShader;
    125.             }
    126.         }
    127.      
    128.         // Shared (for this component) replacement material for opaque geometry highlighting
    129.         private Material _opaqueMaterial;
    130.         private Material opaqueMaterial
    131.         {
    132.             get
    133.             {
    134.                 if (_opaqueMaterial == null)
    135.                 {
    136.                     _opaqueMaterial = new Material(opaqueShader);
    137.                     // Make sure that shader will have proper default value
    138.                     if (cachedSeeThrough) { _opaqueMaterial.EnableKeyword(keywordSeeThrough); }
    139.                     else { _opaqueMaterial.DisableKeyword(keywordSeeThrough); }
    140.                 }
    141.                 return _opaqueMaterial;
    142.             }
    143.         }
    144.         #endregion
    145.         #region MonoBehaviour
    146.         //
    147.         void Awake()
    148.         {
    149.             tr = GetComponent<Transform>();
    150.             renderersDirty = true;
    151.             cachedSeeThrough = seeThrough = true;
    152.             mode = Mode.None;
    153.             // Initial highlighting state
    154.             once = false;
    155.             flashing = false;
    156.             occluder = false;
    157.             transitionValue = transitionTarget = 0f;
    158.             onceColor = Color.red;
    159.             flashingFreq = 2f;
    160.             flashingColorMin = new Color(0f, 1f, 1f, 0f);
    161.             flashingColorMax = new Color(0f, 1f, 1f, 1f);
    162.             constantColor = Color.yellow;
    163.         }
    164.      
    165.         //
    166.         void OnEnable()
    167.         {
    168.             highlighters.Add(this);
    169.         }
    170.         //
    171.         void OnDisable()
    172.         {
    173.             highlighters.Remove(this);
    174.          
    175.             ClearRenderers();
    176.             // Reset highlighting parameters to default values
    177.             renderersDirty = true;
    178.             once = false;
    179.             flashing = false;
    180.             transitionValue = transitionTarget = 0f;
    181.             /*
    182.             // Reset custom parameters of the highlighting
    183.             occluder = false;
    184.             seeThrough = false;
    185.             onceColor = Color.red;
    186.             flashingFreq = 2f;
    187.             flashingColorMin = new Color(0f, 1f, 1f, 0f);
    188.             flashingColorMax = new Color(0f, 1f, 1f, 1f);
    189.             constantColor = Color.yellow;
    190.             transitionTime = 0f;
    191.             */
    192.         }
    193.         //
    194.         void Update()
    195.         {
    196.             UpdateTransition();
    197.         }
    198.         //
    199.         void OnDestroy()
    200.         {
    201.             // Unity never garbage-collects unreferenced materials, so it is our responsibility to destroy them
    202.             if (_opaqueMaterial != null)
    203.             {
    204.                 Destroy(_opaqueMaterial);
    205.             }
    206.         }
    207.         #endregion
    208.         #region Private Methods
    209.         // Clear cached renderers
    210.         private void ClearRenderers()
    211.         {
    212.             for (int i = highlightableRenderers.Count - 1; i >= 0; i--)
    213.             {
    214.                 HighlighterRenderer renderer = highlightableRenderers[i];
    215.                 renderer.SetState(false);
    216.             }
    217.             highlightableRenderers.Clear();
    218.         }
    219.         // This method defines the way in which renderers are initialized
    220.         private void UpdateRenderers()
    221.         {
    222.             if (renderersDirty)
    223.             {
    224.                 ClearRenderers();
    225.                 // Find all renderers which should be controlled with this Highlighter component
    226.                 List<Renderer> renderers = new List<Renderer>();
    227.                 GrabRenderers(tr, renderers);
    228.                 // Cache found renderers
    229.                 for (int i = 0, imax = renderers.Count; i < imax; i++)
    230.                 {
    231.                     GameObject rg = renderers[i].gameObject;
    232.                     HighlighterRenderer renderer = rg.GetComponent<HighlighterRenderer>();
    233.                     if (renderer == null) { renderer = rg.AddComponent<HighlighterRenderer>(); }
    234.                     renderer.SetState(true);
    235.                     renderer.Initialize(opaqueMaterial, transparentShader, cachedSeeThrough);
    236.                     highlightableRenderers.Add(renderer);
    237.                 }
    238.              
    239.                 renderersDirty = false;
    240.             }
    241.         }
    242.         // Recursively follows hierarchy of objects from t, searches for Renderers and adds them to the list.
    243.         // Breaks if HighlighterBlocker or another Highlighter component found
    244.         private void GrabRenderers(Transform t, List<Renderer> renderers)
    245.         {
    246.             GameObject g = t.gameObject;
    247.             // Find all Renderers of all types on current GameObject g and add them to the renderers list
    248.             for (int i = 0, imax = types.Count; i < imax; i++)
    249.             {
    250.                 g.GetComponents(types[i], sComponents);
    251.                 for (int j = 0, jmax = sComponents.Count; j < jmax; j++)
    252.                 {
    253.                     renderers.Add(sComponents[j] as Renderer);
    254.                 }
    255.             }
    256.             sComponents.Clear();
    257.          
    258.             // Return if transform t doesn't have any children
    259.             int childCount = t.childCount;
    260.             if (childCount == 0) { return; }
    261.          
    262.             // Recursively cache renderers on all child GameObjects
    263.             for (int i = 0; i < childCount; i++)
    264.             {
    265.                 Transform childTransform = t.GetChild(i);
    266.                 // Do not cache Renderers of this childTransform in case it has it's own Highlighter component
    267.                 Highlighter h = childTransform.GetComponent<Highlighter>();
    268.                 if (h != null) { continue; }
    269.                 // Do not cache Renderers of this childTransform in case HighlighterBlocker found
    270.                 HighlighterBlocker hb = childTransform.GetComponent<HighlighterBlocker>();
    271.                 if (hb != null) { continue; }
    272.              
    273.                 GrabRenderers(childTransform, renderers);
    274.             }
    275.         }
    276.         // Updates highlighting color
    277.         private void UpdateColors()
    278.         {
    279.             if (once)
    280.             {
    281.                 currentColor = onceColor;
    282.             }
    283.             else if (flashing)
    284.             {
    285.                 // Flashing frequency is not affected by Time.timeScale
    286.                 currentColor = Color.Lerp(flashingColorMin, flashingColorMax, 0.5f * Mathf.Sin(Time.realtimeSinceStartup * flashingFreq * doublePI) + 0.5f);
    287.             }
    288.             else if (transitionValue > 0f)
    289.             {
    290.                 currentColor = constantColor;
    291.                 currentColor.a *= transitionValue;
    292.             }
    293.             else if (occluder)
    294.             {
    295.                 currentColor = occluderColor;
    296.             }
    297.             else
    298.             {
    299.                 return;
    300.             }
    301.             // Apply color
    302.             opaqueMaterial.SetColor(shaderPropertyID._Color, currentColor);
    303.             for (int i = 0; i < highlightableRenderers.Count; i++)
    304.             {
    305.                 highlightableRenderers[i].SetColorForTransparent(currentColor);
    306.             }
    307.         }
    308.         // Update transition value
    309.         private void UpdateTransition()
    310.         {
    311.             if (transitionValue != transitionTarget)
    312.             {
    313.                 if (transitionTime <= 0f)
    314.                 {
    315.                     transitionValue = transitionTarget;
    316.                 }
    317.                 else
    318.                 {
    319.                     float dir = (transitionTarget > 0f ? 1f : -1f);
    320.                     transitionValue = Mathf.Clamp01(transitionValue + (dir * Time.unscaledDeltaTime) / transitionTime);
    321.                 }
    322.             }
    323.         }
    324.         //
    325.         private void FillBufferInternal(CommandBuffer buffer, Mode m)
    326.         {
    327.             UpdateRenderers();
    328.             // Update mode
    329.             mode = Mode.None;
    330.             // Highlighter
    331.             if (once || flashing || (transitionValue > 0f))
    332.             {
    333.                 mode = seeThrough ? Mode.HighlighterSeeThrough : Mode.Highlighter;
    334.             }
    335.             // Occluder
    336.             else if (occluder && seeThrough)
    337.             {
    338.                 mode = Mode.OccluderSeeThrough;
    339.             }
    340.             if (mode == Mode.None || mode != m) { return; }
    341.             // Update shader property if changed
    342.             if (cachedSeeThrough != seeThrough)
    343.             {
    344.                 cachedSeeThrough = seeThrough;
    345.                 if (cachedSeeThrough) { _opaqueMaterial.EnableKeyword(keywordSeeThrough); }
    346.                 else { _opaqueMaterial.DisableKeyword(keywordSeeThrough); }
    347.                 for (int i = 0; i < highlightableRenderers.Count; i++)
    348.                 {
    349.                     highlightableRenderers[i].SetSeeThroughForTransparent(seeThrough);
    350.                 }
    351.             }
    352.             UpdateColors();
    353.             // Fill CommandBuffer with this highlighter rendering commands
    354.             for (int i = highlightableRenderers.Count - 1; i >= 0; i--)
    355.             {
    356.                 // To avoid null-reference exceptions when cached renderer has been removed but ReinitMaterials wasn't been called
    357.                 HighlighterRenderer renderer = highlightableRenderers[i];
    358.                 if (renderer == null)
    359.                 {
    360.                     highlightableRenderers.RemoveAt(i);
    361.                 }
    362.                 // Try to fill buffer
    363.                 else if (!renderer.FillBuffer(buffer, forceRender))
    364.                 {
    365.                     highlightableRenderers.RemoveAt(i);
    366.                     renderer.SetState(false);
    367.                 }
    368.             }
    369.         }
    370.         #endregion
    371.         #region Static Methods
    372.         // Fill CommandBuffer with highlighters rendering commands
    373.         static public void FillBuffer(CommandBuffer buffer)
    374.         {
    375.             for (int i = 0; i < renderingOrder.Length; i++)
    376.             {
    377.                 Mode mode = renderingOrder[i];
    378.                 var e = highlighters.GetEnumerator();
    379.                 while (e.MoveNext())
    380.                 {
    381.                     Highlighter highlighter = e.Current;
    382.                     highlighter.FillBufferInternal(buffer, mode);
    383.                 }
    384.             }
    385.         }
    386.         #endregion
    387.     }
    388. }
    389.  
     
  12. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Okay. So I see the definition for ConstantOnImmediate. I also see another method that would be used to turn off the highlighting. Try writing the code to cycle between calling ConstantOnImmediate and the other method based on the `t % 1f` I put in my previous reply. Show me what that would look like.
     
  13. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HighlighterSpectrum : HighlighterInteractive
    5. {
    6.     public bool random = true;
    7.     public float velocity = 0.13f;
    8.  
    9.     private float t;
    10.  
    11.     private readonly Color _myColor = new Color (22, 34, 23);
    12.  
    13.  
    14.  
    15.     #region MonoBehaviour
    16.     //
    17.     protected override void Awake()
    18.     {
    19.         base.Awake();
    20.  
    21.         t = random ? Random.value : 0f;
    22.     }
    23.  
    24.     //
    25.     protected override void Update()
    26.     {
    27.         base.Update();
    28.         h.ConstantOnImmediate(t % 1f > 0.5f ? _myColor : Color.black);
    29.         h.ConstantOffImmediate(t % 0f > 0.5f ? _myColor : Color.black);
    30.         t += Time.deltaTime * velocity;
    31.         t %= 1f;
    32.  
    33.    
    34.     }
    35.     #endregion
    36. }
    37.  
     
  14. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Closer, but not quite. Here's some pseudocode. Try to put this into C#:

    1. Every Update, add Time.deltaTime to a variable "_timeSinceLastToggle" This keeps track of how long it's been since the highlighter was last toggled.
    2. Keep track of the current highlight state in a boolean variable named "_isHighlightingOn".
    3. Every update, check if "_timeSinceLastToggle" is greater than a certain value, such as "velocity". If it's less, do nothing, if it's greater, toggle the highlighting.
    4. To toggle, call either ConstantOnImmediate or ConstantOffImmediate depending on whether "_isHighlightingOn" is true or false.
    5. After toggling, invert the value of _isHighlightingOn, and reset _timeSinceLastToggle to 0.
     
    davejones1 likes this.
  15. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Thanks for the response. This is something I will try.
     
    Last edited: Mar 25, 2018