IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
Widget.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using UnityEngine.EventSystems;
4 using System.Collections;
5 using System.Collections.Generic;
6 
7 
8 namespace UI
9 {
10  public class Widget : MonoBehaviour
11  {
12  public LayoutPosition layoutPosition{ private set; get; }
13  public Screen layoutScreen = Screen.center;
14  public AlignmentH layoutAlignHorizontal = AlignmentH.stretch;
15  public AlignmentV layoutAlignVertical = AlignmentV.stretch;
16  [Range(0, 99)] public int layoutLayer = 0;
17 
18  private Material textMaterial = null;
19  private Material imageMaterial = null;
20 
21  public void Awake()
22  {
23  // Make sure my canvas is centered:
24  Canvas cv = GetComponentInChildren (typeof(Canvas), true) as Canvas;
25  if (cv != null) {
26  cv.transform.localPosition = Vector3.zero;
27  }
28 
29  UpdateMaterials ();
30  }
31 
32  public void UpdateMaterials()
33  {
34  if( textMaterial == null )
35  textMaterial = new Material (Shader.Find ("Custom/TextShader"));
36  if( imageMaterial == null )
37  imageMaterial = new Material (Shader.Find ("Custom/UIObject"));
38 
39  // Set material for all texts:
40  Component[] texts;
41  texts = GetComponentsInChildren (typeof(Text), true);
42  if (texts != null) {
43  foreach (Text t in texts)
44  t.material = textMaterial;
45  }
46 
47  // Set material for all images:
48  Component[] images;
49  images = GetComponentsInChildren (typeof(Image), true);
50  if (images != null) {
51  foreach (Image i in images) {
52  if( i.material.name == "Default UI Material")
53  i.material = imageMaterial;
54  }
55  }
56  }
57 
58  public void OnEnable()
59  {
60  layoutPosition = new LayoutPosition();
61  layoutPosition.screen = layoutScreen;
62  layoutPosition.alignHorizontal = layoutAlignHorizontal;
63  layoutPosition.alignVertical = layoutAlignVertical;
64  UI.Core.instance.layoutSystem.addWidget (this);
65  }
66 
67  public void OnDisable()
68  {
69  UI.Core.instance.layoutSystem.removeWidget (this);
70  }
71 
72  public void OnDestroy()
73  {
74  UI.Core.instance.layoutSystem.removeWidget (this);
75  }
76 
77  public void setPosition( LayoutPosition newPosition )
78  {
79  layoutPosition = newPosition;
80  UI.Core.instance.layoutSystem.setWidgetPosition( this, newPosition );
81  }
82 
83  public void highlight()
84  {
85  imageMaterial.SetInt ("_highlight", 1);
86 
87  //GetComponent<RectTransform> ().localScale = activeScale;
88  Vector3 pos = transform.localPosition;
89  pos.z = 100 - layoutLayer;
90  transform.localPosition = pos;
91  }
92 
93  public void unHighlight()
94  {
95  imageMaterial.SetInt ("_highlight", 0);
96 
97  //GetComponent<RectTransform> ().localScale = inactiveScale;
98  Vector3 pos = transform.localPosition;
99  pos.z = 200 - layoutLayer;
100  transform.localPosition = pos;
101  }
102  }
103 }