Author Topic: Problem on updating material properties on UI2DSprite's onRender  (Read 2306 times)

MildMania

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Problem on updating material properties on UI2DSprite's onRender
« on: November 23, 2015, 08:04:50 AM »
Hello there,

I'm trying to assign a material to UI2DSprite on the run and update the material properties. Weird things is, it's only working if I change the parent of objects so I believe when I change the parent it somehow updates something about how widget is drawing and starts to use new material.
I couldn't do it manually, tried MarkAsChanged, MarkParentChanged and all other stuff. Only way to get it working now is when I change the parent of the object. Am I doing something wrong here?

You can find the code below.

  1. using UnityEngine;
  2. using DG.Tweening;
  3.  
  4. public class MaterialBlinkColor
  5. {
  6.     public bool ShouldContinue { get; set; }
  7.     Tweener _curTween;
  8.     Material _material;
  9.     Material _defaultMaterial;
  10.     UI2DSprite _sprite;
  11.     string _property;
  12.  
  13.     public MaterialBlinkColor(UI2DSprite sprite, Material material)
  14.     {
  15.         ShouldContinue = true;
  16.         _defaultMaterial = new Material(material);
  17.         _sprite = sprite;
  18.         _material = new Material(material);
  19.         _sprite.material = _material;
  20.         BlinkSmoothly("_AddColor", _material.GetColor("_AddColor"), new Color(0.85f, 0.85f, 0.85f, 0), 1f);
  21.         //_sprite.MarkAsChanged();
  22.         //_sprite.SetDirty();
  23.         //NGUITools.MarkParentAsChanged(_sprite.transform.parent.gameObject);
  24.     }
  25.  
  26.     public void BlinkSmoothly(string property, Color startColor, Color endColor, float totalDuration, bool runOnce = false)
  27.     {
  28.         //_sprite.GetComponent<Rigidbody2D>().isKinematic = true;
  29.         //_sprite.transform.parent = _sprite.root.transform;
  30.  
  31.         _sprite.onRender = OnRender;
  32.         _property = property;
  33.         if (ShouldContinue)
  34.         {
  35.             _curTween = _material.DOColor(endColor, _property, totalDuration / 2)
  36.                 .OnComplete(new TweenCallback(() => BlinkSmoothly(_property, endColor, startColor, totalDuration, runOnce)))
  37.                 .SetEase(Ease.InOutCubic);
  38.             ShouldContinue &= !runOnce;
  39.         }
  40.     }
  41.  
  42.     void OnRender(Material mat)
  43.     {
  44.         if (!ShouldContinue)
  45.         {
  46.             _sprite.onRender = null;
  47.             _material = _defaultMaterial;
  48.         }
  49.         mat.SetColor(_property, _material.GetColor(_property));
  50.     }
  51. }
  52.  

Thanks,
Burkay

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem on updating material properties on UI2DSprite's onRender
« Reply #1 on: November 25, 2015, 05:30:51 PM »
If you want to change the material properties of a widget, you need to use the UIWidget.onRender callback. It passes the material as a property. NGUI makes copies of materials it works with.