77 * Copyright (c) 2016 by Shen, Jen-Chieh $
88 */
99using UnityEngine ;
10+ using UnityEngine . EventSystems ;
1011using System . Collections ;
1112
1213
1314namespace JCSUnity
1415{
15-
1616 /// <summary>
1717 /// Scale approach to a specific scale value.
1818 /// </summary>
@@ -26,6 +26,25 @@ public class JCS_ScaleEffect
2626 //----------------------
2727 // Private Variables
2828
29+ private Vector3 mTowardScale = Vector3 . zero ;
30+ private Vector3 mRecordScale = Vector3 . zero ;
31+ private Vector3 mTargetScale = Vector3 . zero ;
32+
33+
34+ [ Header ( "** Check Variables (JCS_ScaleEffect) **" ) ]
35+
36+ [ SerializeField ]
37+ private bool mEffect = false ;
38+
39+ [ Tooltip ( "" ) ]
40+ [ SerializeField ]
41+ private JCS_PanelRoot mPanelRoot = null ;
42+
43+ [ Tooltip ( "" ) ]
44+ [ SerializeField ]
45+ private EventTrigger mEventTrigger = null ;
46+
47+
2948 [ Header ( "** Initialize Variables (JCS_ScaleEffect) **" ) ]
3049
3150 [ Tooltip ( "Do scale in x axis." ) ]
@@ -48,11 +67,21 @@ public class JCS_ScaleEffect
4867 [ SerializeField ]
4968 private Vector3 mScaleFriction = new Vector3 ( 0.2f , 0.2f , 0.2f ) ;
5069
51- private Vector3 mTowardScale = Vector3 . zero ;
52- private Vector3 mRecordScale = Vector3 . zero ;
53- private Vector3 mTargetScale = Vector3 . zero ;
5470
55- private bool mEffect = false ;
71+ [ Header ( "- UI (JCS_ScaleEffect)" ) ]
72+
73+ [ Tooltip ( "Add event to event trigger system!" ) ]
74+ [ SerializeField ]
75+ private bool mAutoAddEvent = true ;
76+
77+ [ Tooltip ( "Event trigger type to active the the slide effect." ) ]
78+ [ SerializeField ]
79+ private EventTriggerType mActiveEventTriggerType = EventTriggerType . PointerEnter ;
80+
81+ [ Tooltip ( "Event trigger type to deactive the the slide effect." ) ]
82+ [ SerializeField ]
83+ private EventTriggerType mDeactiveEventTriggerType = EventTriggerType . PointerExit ;
84+
5685
5786 //----------------------
5887 // Protected Variables
@@ -69,81 +98,132 @@ public class JCS_ScaleEffect
6998 //------------------------------
7099 private void Start ( )
71100 {
101+ // Only need it for the UI.
102+ if ( GetObjectType ( ) == JCS_UnityObjectType . UI )
103+ {
104+ // Get panel root, in order to calculate the
105+ // correct distance base on the resolution.
106+ mPanelRoot = this . GetComponentInParent < JCS_PanelRoot > ( ) ;
107+
108+ if ( mAutoAddEvent )
109+ {
110+ // Event trigger is the must if we need to add the
111+ // event to event trigger system.
112+ {
113+ this . mEventTrigger = this . GetComponent < EventTrigger > ( ) ;
114+ if ( this . mEventTrigger == null )
115+ this . mEventTrigger = this . gameObject . AddComponent < EventTrigger > ( ) ;
116+ }
117+
118+ JCS_Utility . AddEventTriggerEvent ( mEventTrigger , mActiveEventTriggerType , JCS_OnMouseOver ) ;
119+ JCS_Utility . AddEventTriggerEvent ( mEventTrigger , mDeactiveEventTriggerType , JCS_OnMouseExit ) ;
120+ }
121+ }
122+
72123 Vector3 currentScale = this . transform . localScale ;
73124
74125 // record down the scale.
75126 mRecordScale = currentScale ;
76127 mTargetScale = currentScale ;
77128
129+
78130 SetTargetScale ( ) ;
79131 }
80132
81133 private void Update ( )
82134 {
83135 ScaleEffect ( ) ;
84- JCS_OnMouseExit ( ) ;
85136 }
86137
87138 //========================================
88139 // Self-Define
89140 //------------------------------
90141 //----------------------
91142 // Public Functions
143+
144+ /// <summary>
145+ /// Call it when is on mouse over.
146+ ///
147+ /// Use in inspector for Event Trigger System. (Active)
148+ /// </summary>
149+ public void JCS_OnMouseOver ( PointerEventData data )
150+ {
151+ JCS_OnMouseOver ( ) ;
152+ }
92153 public void JCS_OnMouseOver ( )
93154 {
94- mEffect = true ;
95- mTargetScale = mTowardScale ;
155+ Active ( ) ;
96156 }
97157
158+ /// <summary>
159+ /// Call it When is on mouse exit.
160+ ///
161+ /// Use in inspector for Event Trigger System. (Deactive)
162+ /// </summary>
163+ /// <returns></returns>
164+ public void JCS_OnMouseExit ( PointerEventData data )
165+ {
166+ JCS_OnMouseExit ( ) ;
167+ }
98168 public void JCS_OnMouseExit ( )
99169 {
100- if ( ! mEffect )
101- return ;
102-
103- if ( GetObjectType ( ) == JCS_UnityObjectType . UI )
104- {
105- UpdateUnityData ( ) ;
106- if ( JCS_Utility . MouseOverGUI ( this . mRectTransform ) )
107- return ;
108- }
109-
110- mEffect = false ;
111- mTargetScale = mRecordScale ;
170+ Deactive ( ) ;
112171 }
113172
114173 /// <summary>
115174 /// Active the effect.
116175 /// </summary>
117176 public void Active ( )
118177 {
119- JCS_OnMouseOver ( ) ;
178+ mTargetScale = mTowardScale ;
179+
180+ mEffect = true ;
120181 }
121182
122183 /// <summary>
123184 /// Deactive the effect.
124185 /// </summary>
125186 public void Deactive ( )
126187 {
127- JCS_OnMouseExit ( ) ;
188+ if ( ! mEffect )
189+ return ;
190+
191+ mTargetScale = mRecordScale ;
192+
193+ mEffect = false ;
128194 }
129195
130196 //----------------------
131197 // Protected Functions
132198
133199 //----------------------
134200 // Private Functions
201+
202+ /// <summary>
203+ /// Get the target scale.
204+ /// </summary>
135205 private void SetTargetScale ( )
136206 {
137207 // so it will scale base on the starting scale.
138208 Vector3 newTargetScale = mRecordScale ;
139209
140210 if ( mScaleX )
141211 {
212+ // mPanelRoot will be null is the object isn't
213+ // UI game object.
214+ if ( mPanelRoot != null )
215+ mScaleValue . x /= mPanelRoot . PanelDeltaWidthRatio ;
216+
142217 newTargetScale . x += mScaleValue . x ;
143218 }
144219
145220 if ( mScaleY )
146221 {
222+ // mPanelRoot will be null is the object isn't
223+ // UI game object.
224+ if ( mPanelRoot != null )
225+ mScaleValue . y /= mPanelRoot . PanelDeltaWidthRatio ;
226+
147227 newTargetScale . y += mScaleValue . y ;
148228 }
149229
@@ -156,6 +236,9 @@ private void SetTargetScale()
156236 mTowardScale = newTargetScale ;
157237 }
158238
239+ /// <summary>
240+ /// Do the scale effect.
241+ /// </summary>
159242 private void ScaleEffect ( )
160243 {
161244 Vector3 newScale = this . transform . localScale ;
0 commit comments