RevealGraphic.cs 1.5 KB

12345678910111213141516171819202122232425262728
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Olympus
  4. {
  5. [RequireComponent(typeof(CanvasRenderer))]
  6. public sealed class RevealGraphic : MaskableGraphic
  7. {
  8. public float amount=6;
  9. public int style;
  10. Material owned;
  11. public void Configure(string id,int mode)
  12. {
  13. style=mode;int seed=17;foreach(char c in id)seed=unchecked(seed*31+c);
  14. if(!owned){var shader=Resources.Load<Shader>("Olympus/ReferenceA/CanvasReveal");if(shader)owned=new Material(shader);}
  15. if(owned){material=owned;owned.SetFloat("_Seed",Mathf.Abs(seed%1000));owned.SetFloat("_Mode",mode);owned.SetFloat("_Reveal",amount/100);}
  16. SetVerticesDirty();
  17. }
  18. public void SetAmount(float value){amount=value;if(owned){owned.SetFloat("_Reveal",Mathf.Clamp01(value/100));materialForRendering.SetFloat("_Reveal",Mathf.Clamp01(value/100));}}
  19. protected override void OnPopulateMesh(VertexHelper vh)
  20. {
  21. vh.Clear();var r=rectTransform.rect;
  22. vh.AddVert(new Vector3(r.xMin,r.yMin),color,new Vector2(0,0));vh.AddVert(new Vector3(r.xMin,r.yMax),color,new Vector2(0,1));vh.AddVert(new Vector3(r.xMax,r.yMax),color,new Vector2(1,1));vh.AddVert(new Vector3(r.xMax,r.yMin),color,new Vector2(1,0));
  23. vh.AddTriangle(0,1,2);vh.AddTriangle(0,2,3);
  24. }
  25. protected override void OnDestroy(){if(owned){if(Application.isPlaying)Destroy(owned);else DestroyImmediate(owned);}base.OnDestroy();}
  26. }
  27. }