iTween扩展

在使用iTween时,由于参数很多,作者引入了Hashtable。我们再使用时基本都是用iTween.Hash(...)来添加参数。但是连续的字符串写起来确实有点麻烦。

首先我是看到了tkokof1的这篇博客:iTween那些事儿(二)

里面添加了iTweenHashtable这个类型,用来对iTween做改进。

我在他的基础上做了修改和完善。代码如下:

  1 public class iTweenHashtable
  2 {
  3     public delegate void iTweenCallback();
  4
  5     //public delegate void iTweenCallbackParam(Object param);
  6
  7     public delegate void iTweenCallbackParam(params object[] args);
  8
  9     public delegate void iTweenIntCallback(int param);
 10
 11     public delegate void iTweenFloatCallback(float param);
 12
 13     public delegate void iTweenDoubleCallback(double param);
 14
 15     private Hashtable m_innerTable = new Hashtable();
 16
 17     public iTweenHashtable AudioSource(AudioSource source)
 18     {
 19         m_innerTable["audiosource"] = source;
 20
 21         return this;
 22     }
 23
 24     public iTweenHashtable AudioClip(AudioClip audioclip)
 25     {
 26         m_innerTable["audioclip"] = audioclip;
 27
 28         return this;
 29     }
 30
 31     public iTweenHashtable Volume(float volume)
 32     {
 33         m_innerTable["volume"] = volume;
 34
 35         return this;
 36     }
 37
 38     public iTweenHashtable Volume(double volume)
 39     {
 40         m_innerTable["volume"] = volume;
 41
 42         return this;
 43     }
 44
 45     public iTweenHashtable Pitch(float pitch)
 46     {
 47         m_innerTable["pitch"] = pitch;
 48
 49         return this;
 50     }
 51
 52     public iTweenHashtable Pitch(double pitch)
 53     {
 54         m_innerTable["pitch"] = pitch;
 55
 56         return this;
 57     }
 58
 59     public iTweenHashtable Color(Color color)
 60     {
 61         m_innerTable["color"] = color;
 62
 63         return this;
 64     }
 65
 66     public iTweenHashtable R(float r)
 67     {
 68         m_innerTable["r"] = r;
 69
 70         return this;
 71     }
 72
 73     public iTweenHashtable R(double r)
 74     {
 75         m_innerTable["r"] = r;
 76
 77         return this;
 78     }
 79
 80     public iTweenHashtable G(float g)
 81     {
 82         m_innerTable["g"] = g;
 83
 84         return this;
 85     }
 86
 87     public iTweenHashtable G(double g)
 88     {
 89         m_innerTable["g"] = g;
 90
 91         return this;
 92     }
 93
 94     public iTweenHashtable B(float b)
 95     {
 96         m_innerTable["b"] = b;
 97
 98         return this;
 99     }
100
101     public iTweenHashtable B(double b)
102     {
103         m_innerTable["b"] = b;
104
105         return this;
106     }
107
108     public iTweenHashtable A(float a)
109     {
110         m_innerTable["a"] = a;
111
112         return this;
113     }
114
115     public iTweenHashtable A(double a)
116     {
117         m_innerTable["a"] = a;
118
119         return this;
120     }
121
122     public iTweenHashtable NamedColorValue(iTween.NamedValueColor value)
123     {
124         m_innerTable["namedcolorvalue"] = value;
125
126         return this;
127     }
128
129     public iTweenHashtable NamedColorValue(string value)
130     {
131         m_innerTable["namedcolorvalue"] = value;
132
133         return this;
134     }
135
136     public iTweenHashtable IncludeChildren(bool includechildren)
137     {
138         m_innerTable["includechildren"] = includechildren;
139
140         return this;
141     }
142
143     public iTweenHashtable Alpha(float alpha)
144     {
145         m_innerTable["alpha"] = alpha;
146
147         return this;
148     }
149
150     public iTweenHashtable Alpha(double alpha)
151     {
152         m_innerTable["alpha"] = alpha;
153
154         return this;
155     }
156
157     public iTweenHashtable LookTarget(Transform looktarget)
158     {
159         m_innerTable["looktarget"] = looktarget;
160
161         return this;
162     }
163
164     public iTweenHashtable LookTarget(Vector3 looktarget)
165     {
166         m_innerTable["looktarget"] = looktarget;
167
168         return this;
169     }
170
171     public iTweenHashtable Axis(string axis)
172     {
173         m_innerTable["axis"] = axis;
174
175         return this;
176     }
177
178     public iTweenHashtable OrientToPath(bool orienttopath)
179     {
180         m_innerTable["orienttopath"] = orienttopath;
181
182         return this;
183     }
184
185     public iTweenHashtable LookTime(float looktime)
186     {
187         m_innerTable["looktime"] = looktime;
188
189         return this;
190     }
191
192     public iTweenHashtable LookTime(double looktime)
193     {
194         m_innerTable["looktime"] = looktime;
195
196         return this;
197     }
198
199     public iTweenHashtable Name(string name)
200     {
201         m_innerTable["name"] = name;
202
203         return this;
204     }
205
206     public iTweenHashtable From(float val)
207     {
208         m_innerTable["from"] = val;
209
210         return this;
211     }
212
213     public iTweenHashtable From(double val)
214     {
215         m_innerTable["from"] = val;
216
217         return this;
218     }
219
220     public iTweenHashtable From(Vector3 val)
221     {
222         m_innerTable["from"] = val;
223
224         return this;
225     }
226
227     public iTweenHashtable From(Vector2 val)
228     {
229         m_innerTable["from"] = val;
230
231         return this;
232     }
233
234     public iTweenHashtable From(Color val)
235     {
236         m_innerTable["from"] = val;
237
238         return this;
239     }
240
241     public iTweenHashtable From(Rect val)
242     {
243         m_innerTable["from"] = val;
244
245         return this;
246     }
247
248     public iTweenHashtable To(float val)
249     {
250         m_innerTable["to"] = val;
251
252         return this;
253     }
254
255     public iTweenHashtable To(double val)
256     {
257         m_innerTable["to"] = val;
258
259         return this;
260     }
261
262     public iTweenHashtable To(Vector3 val)
263     {
264         m_innerTable["to"] = val;
265
266         return this;
267     }
268
269     public iTweenHashtable To(Vector2 val)
270     {
271         m_innerTable["to"] = val;
272
273         return this;
274     }
275
276     public iTweenHashtable To(Color val)
277     {
278         m_innerTable["to"] = val;
279
280         return this;
281     }
282
283     public iTweenHashtable To(Rect val)
284     {
285         m_innerTable["to"] = val;
286
287         return this;
288     }
289
290     public iTweenHashtable Amount(Vector3 amount)
291     {
292         m_innerTable["amount"] = amount;
293
294         return this;
295     }
296
297     public iTweenHashtable X(float x)
298     {
299         m_innerTable["x"] = x;
300
301         return this;
302     }
303
304     public iTweenHashtable X(double x)
305     {
306         m_innerTable["x"] = x;
307
308         return this;
309     }
310
311     public iTweenHashtable Y(float y)
312     {
313         m_innerTable["y"] = y;
314
315         return this;
316     }
317
318     public iTweenHashtable Y(double y)
319     {
320         m_innerTable["y"] = y;
321
322         return this;
323     }
324
325     public iTweenHashtable Z(float z)
326     {
327         m_innerTable["z"] = z;
328
329         return this;
330     }
331
332     public iTweenHashtable Z(double z)
333     {
334         m_innerTable["z"] = z;
335
336         return this;
337     }
338
339     public iTweenHashtable Space(Space space)
340     {
341         m_innerTable["space"] = space;
342
343         return this;
344     }
345
346     public iTweenHashtable Position(Vector3 position)
347     {
348         m_innerTable["position"] = position;
349
350         return this;
351     }
352
353     public iTweenHashtable Position(Transform position)
354     {
355         m_innerTable["position"] = position;
356
357         return this;
358     }
359
360     public iTweenHashtable Path(Vector3 path)
361     {
362         m_innerTable["path"] = path;
363
364         return this;
365     }
366
367     public iTweenHashtable Path(Transform path)
368     {
369         m_innerTable["path"] = path;
370
371         return this;
372     }
373
374     public iTweenHashtable Lookahead(float lookahead)
375     {
376         m_innerTable["lookahead"] = lookahead;
377
378         return this;
379     }
380
381     public iTweenHashtable Lookahead(double lookahead)
382     {
383         m_innerTable["lookahead"] = lookahead;
384
385         return this;
386     }
387
388     public iTweenHashtable MoveToPath(bool movetopath)
389     {
390         m_innerTable["movetopath"] = movetopath;
391
392         return this;
393     }
394
395     public iTweenHashtable Rotation(Vector3 rotation)
396     {
397         m_innerTable["rotation"] = rotation;
398
399         return this;
400     }
401
402     public iTweenHashtable Rotation(Transform rotation)
403     {
404         m_innerTable["rotation"] = rotation;
405
406         return this;
407     }
408
409     public iTweenHashtable Scale(Vector3 scale)
410     {
411         m_innerTable["scale"] = scale;
412
413         return this;
414     }
415
416     public iTweenHashtable IsLocal(bool isLocal)
417     {
418         m_innerTable["islocal"] = isLocal;
419
420         return this;
421     }
422
423     public iTweenHashtable Time(float time)
424     {
425         m_innerTable["time"] = time;
426
427         return this;
428     }
429
430     public iTweenHashtable Time(double time)
431     {
432         m_innerTable["time"] = time;
433
434         return this;
435     }
436
437     public iTweenHashtable Speed(float speed)
438     {
439         m_innerTable["speed"] = speed;
440
441         return this;
442     }
443
444     public iTweenHashtable Speed(double speed)
445     {
446         m_innerTable["speed"] = speed;
447
448         return this;
449     }
450
451     public iTweenHashtable Delay(float delay)
452     {
453         m_innerTable["delay"] = delay;
454
455         return this;
456     }
457
458     public iTweenHashtable Delay(double delay)
459     {
460         m_innerTable["delay"] = delay;
461
462         return this;
463     }
464
465     public iTweenHashtable EaseType(iTween.EaseType easeType)
466     {
467         m_innerTable["easetype"] = easeType;
468
469         return this;
470     }
471
472     public iTweenHashtable LoopType(iTween.LoopType loopType)
473     {
474         m_innerTable["looptype"] = loopType;
475
476         return this;
477     }
478
479     public iTweenHashtable OnStart(iTweenCallback onStart)
480     {
481         m_innerTable["onstart"] = onStart.Method.Name;
482
483         MonoBehaviour target = onStart.Target as MonoBehaviour;
484
485         if (target == null)
486         {
487             Debug.LogError("onStart target cannot be null");
488             return this;
489         }
490
491         m_innerTable["onstarttarget"] = target.gameObject;
492
493         return this;
494     }
495
496     public iTweenHashtable OnStart(iTweenCallbackParam onStart, Object param)
497     {
498         m_innerTable["onstart"] = onStart.Method.Name;
499
500         MonoBehaviour target = onStart.Target as MonoBehaviour;
501
502         if (target == null)
503         {
504             Debug.LogError("onStart target cannot be null");
505             return this;
506         }
507
508         m_innerTable["onstarttarget"] = target.gameObject;
509
510         // NOTE: seems iTween can not handle this correct ...
511         // in iTween.CleanArgs, it just do raw element access
512
513         if (param == null)
514         {
515             Debug.LogError("onStart param cannot be null");
516             return this;
517         }
518
519         m_innerTable["onstartparams"] = param;
520
521         return this;
522     }
523
524     public iTweenHashtable OnStart(iTweenIntCallback onStart, int param)
525     {
526         m_innerTable["onstart"] = onStart.Method.Name;
527
528         MonoBehaviour target = onStart.Target as MonoBehaviour;
529
530         if (target == null)
531         {
532             Debug.LogError("onStart target cannot be null");
533             return this;
534         }
535
536         m_innerTable["onstarttarget"] = target.gameObject;
537
538         m_innerTable["onstartparams"] = param;
539
540         return this;
541     }
542
543     public iTweenHashtable OnStart(iTweenFloatCallback onStart, float param)
544     {
545         m_innerTable["onstart"] = onStart.Method.Name;
546
547         MonoBehaviour target = onStart.Target as MonoBehaviour;
548
549         if (target == null)
550         {
551             Debug.LogError("onStart target cannot be null");
552             return this;
553         }
554
555         m_innerTable["onstarttarget"] = target.gameObject;
556
557         m_innerTable["onstartparams"] = param;
558
559         return this;
560     }
561
562     public iTweenHashtable OnStart(iTweenDoubleCallback onStart, double param)
563     {
564         m_innerTable["onstart"] = onStart.Method.Name;
565
566         MonoBehaviour target = onStart.Target as MonoBehaviour;
567
568         if (target == null)
569         {
570             Debug.LogError("onStart target cannot be null");
571             return this;
572         }
573
574         m_innerTable["onstarttarget"] = target.gameObject;
575
576         m_innerTable["onstartparams"] = param;
577
578         return this;
579     }
580
581     public iTweenHashtable OnUpdate()
582     {
583         m_innerTable["onupdate"] = string.Empty;
584         return this;
585     }
586
587     public iTweenHashtable OnUpdate(iTweenCallback onUpdate)
588     {
589         m_innerTable["onupdate"] = onUpdate.Method.Name;
590
591         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
592
593         if (target == null)
594         {
595             Debug.LogError("onUpdate target cannot be null");
596             return this;
597         }
598
599         m_innerTable["onupdatetarget"] = target.gameObject;
600
601         return this;
602     }
603
604     public iTweenHashtable OnUpdate(iTweenFloatCallback onUpdate)
605     {
606         m_innerTable["onupdate"] = onUpdate.Method.Name;
607
608         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
609
610         if (target == null)
611         {
612             Debug.LogError("onUpdate target cannot be null");
613             return this;
614         }
615
616         m_innerTable["onupdatetarget"] = target.gameObject;
617
618         return this;
619     }
620
621     public iTweenHashtable OnUpdate(iTweenCallbackParam onUpdate, Object param)
622     {
623         m_innerTable["onupdate"] = onUpdate.Method.Name;
624
625         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
626
627         if (target == null)
628         {
629             Debug.LogError("onUpdate target cannot be null");
630             return this;
631         }
632
633         m_innerTable["onupdatetarget"] = target.gameObject;
634
635
636         // NOTE: seems iTween can not handle this correct ...
637         // in iTween.CleanArgs, it just do raw element access
638
639         if (param == null)
640         {
641             Debug.LogError("onUpdate param cannot be null");
642             return this;
643         }
644
645         m_innerTable["onupdateparams"] = param;
646
647         return this;
648     }
649
650     public iTweenHashtable OnUpdate(iTweenIntCallback onUpdate, int param)
651     {
652         m_innerTable["onupdate"] = onUpdate.Method.Name;
653
654         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
655
656         if (target == null)
657         {
658             Debug.LogError("onUpdate target cannot be null");
659             return this;
660         }
661
662         m_innerTable["onupdatetarget"] = target.gameObject;
663
664         m_innerTable["onupdateparams"] = param;
665
666         return this;
667     }
668
669     public iTweenHashtable OnUpdate(iTweenFloatCallback onUpdate, float param)
670     {
671         m_innerTable["onupdate"] = onUpdate.Method.Name;
672
673         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
674
675         if (target == null)
676         {
677             Debug.LogError("onUpdate target cannot be null");
678             return this;
679         }
680
681         m_innerTable["onupdatetarget"] = target.gameObject;
682
683         m_innerTable["onupdateparams"] = param;
684
685         return this;
686     }
687
688     public iTweenHashtable OnUpdate(iTweenDoubleCallback onUpdate, double param)
689     {
690         m_innerTable["onupdate"] = onUpdate.Method.Name;
691
692         MonoBehaviour target = onUpdate.Target as MonoBehaviour;
693
694         if (target == null)
695         {
696             Debug.LogError("onUpdate target cannot be null");
697             return this;
698         }
699
700         m_innerTable["onupdatetarget"] = target.gameObject;
701
702         m_innerTable["onupdateparams"] = param;
703
704         return this;
705     }
706
707     public iTweenHashtable OnComplete(iTweenCallback onComplete)
708     {
709         m_innerTable["oncomplete"] = onComplete.Method.Name;
710
711         MonoBehaviour target = onComplete.Target as MonoBehaviour;
712
713         if (target == null)
714         {
715             Debug.LogError("onComplete target cannot be null");
716             return this;
717         }
718
719         m_innerTable["oncompletetarget"] = target.gameObject;
720
721         return this;
722     }
723
724     public iTweenHashtable OnComplete(iTweenCallbackParam onComplete, Object param)
725     {
726         m_innerTable["oncomplete"] = onComplete.Method.Name;
727
728         MonoBehaviour target = onComplete.Target as MonoBehaviour;
729
730         if (target == null)
731         {
732             Debug.LogError("onComplete target cannot be null");
733             return this;
734         }
735
736         m_innerTable["oncompletetarget"] = target.gameObject;
737
738
739         // NOTE: seems iTween can not handle this correct ...
740         //in iTween.CleanArgs, it just do raw element access
741
742         if (param == null)
743         {
744             Debug.LogError("onComplete param cannot be null");
745             return this;
746         }
747
748         m_innerTable["oncompleteparams"] = param;
749
750         return this;
751     }
752
753     public iTweenHashtable OnComplete(iTweenIntCallback onComplete, int param)
754     {
755         m_innerTable["oncomplete"] = onComplete.Method.Name;
756
757         MonoBehaviour target = onComplete.Target as MonoBehaviour;
758
759         if (target == null)
760         {
761             Debug.LogError("onComplete target cannot be null");
762             return this;
763         }
764
765         m_innerTable["oncompletetarget"] = target.gameObject;
766
767         m_innerTable["oncompleteparams"] = param;
768
769         return this;
770     }
771
772     public iTweenHashtable OnComplete(iTweenFloatCallback onComplete, float param)
773     {
774         m_innerTable["oncomplete"] = onComplete.Method.Name;
775
776         MonoBehaviour target = onComplete.Target as MonoBehaviour;
777
778         if (target == null)
779         {
780             Debug.LogError("onComplete target cannot be null");
781             return this;
782         }
783
784         m_innerTable["oncompletetarget"] = target.gameObject;
785
786         m_innerTable["oncompleteparams"] = param;
787
788         return this;
789     }
790
791     public iTweenHashtable OnComplete(iTweenDoubleCallback onComplete, double param)
792     {
793         m_innerTable["oncomplete"] = onComplete.Method.Name;
794
795         MonoBehaviour target = onComplete.Target as MonoBehaviour;
796
797         if (target == null)
798         {
799             Debug.LogError("onComplete target cannot be null");
800             return this;
801         }
802
803         m_innerTable["oncompletetarget"] = target.gameObject;
804
805         m_innerTable["oncompleteparams"] = param;
806
807         return this;
808     }
809
810     public iTweenHashtable IgnoreTimeScale(bool ignoreTimeScale)
811     {
812         m_innerTable["ignoretimescale"] = ignoreTimeScale;
813
814         return this;
815     }
816
817     public void Clear()
818     {
819         m_innerTable.Clear();
820     }
821
822     public static implicit operator Hashtable(iTweenHashtable table)
823     {
824         return table.m_innerTable;
825     }
826 }

iTweenHashtable

接下来是一个工具类:

  1 public static class iTweenEx
  2 {
  3     /// <summary>
  4     /// 计时委托
  5     /// </summary>
  6     /// <param name="target">脚本所在对象</param>
  7     /// <param name="time">计时时长(秒)</param>
  8     /// <param name="onComplete">计时结束要执行的委托</param>
  9     public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onComplete)
 10     {
 11         iTweenHashtable hash = new iTweenHashtable();
 12         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnUpdate();
 13         iTween.ValueTo(target, hash);
 14     }
 15
 16     /// <summary>
 17     /// 计时委托
 18     /// </summary>
 19     /// <param name="target">脚本所在对象</param>
 20     /// <param name="time">计时时长(秒)</param>
 21     /// <param name="onStart">计时开始要执行的委托</param>
 22     /// <param name="onComplete">计时结束要执行的委托</param>
 23     public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onStart,
 24         iTweenHashtable.iTweenCallback onComplete)
 25     {
 26         iTweenHashtable hash = new iTweenHashtable();
 27         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnStart(onStart).OnUpdate();
 28         iTween.ValueTo(target, hash);
 29     }
 30
 31     /// <summary>
 32     /// 计时委托
 33     /// </summary>
 34     /// <param name="target">脚本所在对象</param>
 35     /// <param name="time">计时时长(秒)</param>
 36     /// <param name="onStart">计时开始要执行的委托</param>
 37     /// <param name="onComplete">计时结束要执行的委托</param>
 38     /// <param name="onUpdate">计时中要执行的委托</param>
 39     public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onStart,
 40         iTweenHashtable.iTweenCallback onComplete,
 41         iTweenHashtable.iTweenCallback onUpdate)
 42     {
 43         iTweenHashtable hash = new iTweenHashtable();
 44         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnStart(onStart).OnUpdate(onUpdate);
 45         iTween.ValueTo(target, hash);
 46     }
 47
 48     /// <summary>
 49     /// 计时委托
 50     /// </summary>
 51     /// <param name="target">脚本所在对象</param>
 52     /// <param name="time">计时时长(秒)</param>
 53     /// <param name="onStart">计时开始要执行的委托</param>
 54     /// <param name="onComplete">计时结束要执行的委托</param>
 55     /// <param name="onUpdate">计时中要执行的委托</param>
 56     public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onStart,
 57         iTweenHashtable.iTweenCallback onComplete,
 58         iTweenHashtable.iTweenFloatCallback onUpdate)
 59     {
 60         iTweenHashtable hash = new iTweenHashtable();
 61         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnStart(onStart).OnUpdate(onUpdate);
 62         iTween.ValueTo(target, hash);
 63     }
 64
 65     /// <summary>
 66     /// 计时委托
 67     /// </summary>
 68     /// <param name="target">脚本所在对象</param>
 69     /// <param name="time">计时时长(秒)</param>
 70     /// <param name="onComplete">计时结束要执行的委托</param>
 71     /// <param name="onUpdate">计时中要执行的委托</param>
 72     public static void CountExecute(GameObject target, float time,iTweenHashtable.iTweenCallback onComplete,
 73         iTweenHashtable.iTweenFloatCallback onUpdate)
 74     {
 75         iTweenHashtable hash = new iTweenHashtable();
 76         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnUpdate(onUpdate);
 77         iTween.ValueTo(target, hash);
 78     }
 79
 80     /// <summary>
 81     /// 计时委托
 82     /// </summary>
 83     /// <param name="target">脚本所在对象</param>
 84     /// <param name="time">计时时长(秒)</param>
 85     /// <param name="delay">延迟时长(秒),即开始计时之前的时长</param>
 86     /// <param name="onStart">计时开始要执行的委托</param>
 87     /// <param name="onComplete">计时结束要执行的委托</param>
 88     public static void CountExecute(GameObject target, float time, float delay, iTweenHashtable.iTweenCallback onStart,
 89         iTweenHashtable.iTweenCallback onComplete)
 90     {
 91         iTweenHashtable hash = new iTweenHashtable();
 92         hash.From(0).To(time).Time(time).Delay(delay).OnComplete(onComplete).OnStart(onStart).OnUpdate();
 93         iTween.ValueTo(target, hash);
 94     }
 95
 96
 97     ///// <summary>
 98     ///// 计时委托
 99     ///// </summary>
100     ///// <param name="target">脚本所在对象</param>
101     ///// <param name="time">计时时长</param>
102     ///// <param name="onUpdate">计时委托</param>
103     //public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onUpdate)
104     //{
105     //    iTweenHashtable hash = new iTweenHashtable();
106     //    hash.From(0).To(time).Time(time).OnUpdate(onUpdate);
107     //    iTween.ValueTo(target, hash);
108     //}
109
110     /// <summary>
111     /// 计时委托
112     /// </summary>
113     /// <param name="target">脚本所在对象</param>
114     /// <param name="time">计时时长</param>
115     /// <param name="onUpdate">计时委托</param>
116     public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenFloatCallback onUpdate)
117     {
118         iTweenHashtable hash = new iTweenHashtable();
119         hash.From(0).To(time).Time(time).OnUpdate(onUpdate);
120         iTween.ValueTo(target, hash);
121     }
122 }

iTweenEx

项目里经常会用到一些小计时。比如3秒后执行一个操作。比如先执行一个操作,完成后在执行一个操作。这里直接用我的工具类就方便多了。

测试类如下:

 1 public class ItweenTest : MonoBehaviour
 2 {
 3     private float count;
 4     private string tip;
 5     private int complextCount;
 6
 7     private void OnGUI()
 8     {
 9         if (GUILayout.Button("Count up to 10"))
10         {
11             iTweenEx.CountExecute(gameObject, 10, Tick);
12         }
13
14         GUILayout.Label("Time elapsed: " + count);
15
16         if (GUILayout.Button("Count down form 10"))
17         {
18             iTweenEx.CountExecute(gameObject, 10, OnStart, OnComplete, OnUpdate);
19         }
20
21         GUILayout.Label(tip);
22
23         if (GUILayout.Button("Complex Count"))
24         {
25             iTweenEx.CountExecute(gameObject, 5, () =>
26             {
27                 complextCount = 0;
28             }, () => iTweenEx.CountExecute(gameObject, 5, time =>
29             {
30                 complextCount = 5 - (int) time;
31             }), time =>
32             {
33                 complextCount = (int) time;
34             });
35         }
36
37         GUILayout.Label("Complex Count: " + complextCount);
38     }
39
40     private void OnUpdate(float time)
41     {
42         tip = "Time left: " + (10 - (int) time);
43     }
44
45     private void OnComplete()
46     {
47         tip = "Done!";
48     }
49
50     private void OnStart()
51     {
52         tip = "Started!";
53     }
54
55
56     private void Tick(float time)
57     {
58         //if ((float) tick - count >= 1)
59         //{
60         //    count++;
61         //}
62         count = (int) time;
63     }
64 }

ItweenTest

第一个按钮是顺数10秒,并显示已过去时间。这里Tick传入的time是从计时开始算起,已过的时间哦。转成整型即为数整秒。

第二个操作是倒数10秒,开始和结束都给出提示,并显示剩余时间。

第三个按钮是先顺数到5,再倒数到0。第三个例子用到了匿名委托和Lambda表达式,而且在iTween里进行了嵌套。是不是很方便?

CountExecute实际上用到的是iTween.ValueTo。在使用ValueTo时必须要有update,于是我在iTweenHashtable里添加了一个空Update

1     public iTweenHashtable OnUpdate()
2     {
3         m_innerTable["onupdate"] = string.Empty;
4         return this;
5     }

iTweenHashtable的使用可以参考上面的工具类。

1 public static void CountExecute(GameObject target, float time, iTweenHashtable.iTweenCallback onComplete)
2     {
3         iTweenHashtable hash = new iTweenHashtable();
4         hash.From(0).To(time).Time(time).OnComplete(onComplete).OnUpdate();
5         iTween.ValueTo(target, hash);
6     }
时间: 2024-08-30 11:36:05

iTween扩展的相关文章

C#中的扩展方法学习总结

??版权声明:本文由秦元培创作和发表,采用署名(BY)-非商业性使用(NC)-相同方式共享(SA)国际许可协议进行许可,转载请注明作者及出处,本文作者为秦元培,本文标题为C#中的扩展方法学习总结,本文链接为http://qinyuanpei.com/2015/12/05/extend-methods-of-csharp/. ??各位朋友大家好,我是秦元培,欢迎大家关注我的博客.最近偶然接触到了C#中的扩展方法,觉得这个语法特性是一个不错的特性,因此决定在这里系统地对C#中的扩展方法相关内容进行下

使用ITweenX 来简化iTween插件的函数调用

?? iTween插件是u3d中很火的动画插件. 但是几乎所有函数的参数都是字符串,函数名也要以字符串的方式传入,这就不好了,我们没有必要去记EaseType和其他的参数串. 对于  设计模式的要求是  多组合,少继承.多扩展,少修改. 由于这个插件本身的功能没有任何问题.所以我们没有必要做任何徒劳的重构.就是扩展就玩了呗. 使用: 把脚本放进Standard Assets/Scripts 等等文件夹中即可; 这也很容易被javascript 或 Boo语言使用. 如果你声明一个 public

Unity插件之-ITween教程1

ITween的官网 http://itween.pixelplacement.com/index.php ITween的官方提供了3种插件,一种是ITween,一个是ITween的各种粒子,最后一个就是ITween的扩展

C#中的扩展方法及用途

GPS平台.网站建设.软件开发.系统运维,找森大网络科技!http://cnsendnet.taobao.com来自森大科技官方博客http://www.cnsendblog.com/index.php/?p=476 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方法的描述,现在我通过一个情景例子来对此进行阐释.假设一个控制台程序cla

iOS8扩展插件开发配置

一.iOS8扩展插件概述 WWDC14除了发布了OS X v10.10和switf外,iOS8.0也开始变得更加开放了.说到开放,当然要数应用扩展(App Extension)了.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他应用程序时使用该项功能,从而实现各个应用程序间的功能和资源共享.可以将扩展理解为一个轻量级(nimble and lightweight)的分身. 以下为常见的三类插件: Target Type Extension point identifi

扩展欧几里得算法的模板实现

我居然现在还记不住扩欧的板子,我太弱啦! 扩展欧几里得算法解决的是这样的问题: 给定一个不定方程组ax+by=gcd(a,b),求他的一组整数解 先给出实现代码 void exgcd(int a,int b,int &x,int &y) { if(!b) { x=1,y=0;//gcd(a,0)显然等于1*a-0*0=a return a; } int ans=exgcd(b,a%b,x,y); int tem=x; x=y; y-=tem-(a/b)*y; return ans;} 但实

c# 扩展方法奇思妙用基础篇五:Dictionary&lt;TKey, TValue&gt; 扩展

Dictionary<TKey, TValue>类是常用的一个基础类,但用起来有时确不是很方便.本文逐一讨论,并使用扩展方法解决. 向字典中添加键和值 添加键和值使用 Add 方法,但很多时候,我们是不敢轻易添加的,因为 Dictionary<TKey, TValue>不允许重复,尝试添加重复的键时 Add 方法引发 ArgumentException. 大多时候,我们都会写成以下的样子: var dict = new Dictionary<int, string>()

C#3.0 扩展方法

扩展方法 在很多时候我们需要编写各种各样的帮助类,因为官方提供的再全面,也会有未包含到的地方,这时一个静态的帮助类就可以帮我们解决问题 举一个不是很恰当的例子,假如要对一个字符串进行验证其内容不为null并且等于admin但是很多地方都要调用,按照封装思想要封装成一个方法,看起来可能是下面这样子的 这并没有任何问题,代码也是比较简单,但是很难会有一种亲近感,因为StringiHelper.ValidArg这样的调用存在.比如这个验证应该是官方应该考虑到的,但是他们忽略了,编写这样一个扩展后会有很

php安装swoole扩展

编译安装 Swoole扩展是按照php标准扩展构建的.使用phpize来生成php编译配置,./configure来做编译配置检测,make进行编译,make install进行安装. 请下载releases版本的swoole,直接从github主干上拉取最新代码可能会编译不过 如果当前用户不是root,可能没有php目录的写权限,安装时需要sudo或者su 如果是在git分支上直接git pull更新代码,重新编译前务必要执行make clean 安装准备 安装swoole前必须保证系统已经安