Unity3d uses NGUI for spherical 3D Rotation UI

Source: Internet
Author: User

Because the work needs to study NGUI to make a spherical three-dimensional rotation UI, initially made a prototype, and now I will share with you, if I have any mistakes or do not do well, please point out and give me some suggestions.

Here I use NGUI of version 2.6, and OnFinished of version 3.0 has a little improvement over version 2.6, as shown below:

void Start (){    TweenScale scale = GetComponent<TweenScale>();    EventDelegate.Add (scale.onFinished, OnFinished);} void OnFinished (){    Debug.Log(TweenScale.current.name);}
Reference from momo http://unity3d.9tech.cn/news/2013/1114/41024.html.

First of all, my materials are made of 5 buttons, so I placed them at the position of the positive Pentagon. As shown in:

Then there are two code classes:

Code Class 1: drag all the buttons, and the main animation operations are also carried out in this class.

Using UnityEngine; using System. collections; // used to mark the position of the UI. That is to say, the front UI is always pos0public enum ThisPos {pos0, pos1, pos2, pos3, pos4} public class RotationNGUI: monoBehaviour {public ThisPos thePos = ThisPos. pos2; // whether to slide bool isRight = false; // whether to slide bool isLeft = false; // float duration = 0.7f; // pre-configure the position according to the UI, and manually drag it to the public Transform tran0; public Transform tran1; public Transform tran2; public Transform tran3; public Transform tran4; // Use this for initializationvoid Start () {// if you do not manually drag the pre-prepared location, you can also obtain it through the pre-created location tag. // tran0 = GameObject. findGameObjectWithTag ("0 "). transform;} // Update is called once per framevoid Update () {UpdateUIState (); OnTweenUI () ;}// Update the UI status, the frontend status is 0 and increases void UpdateUIState () {if (transform. localPosition = tran0.localPosition) {thePos = ThisPos. pos0;} if (transform. localPosition = tran1.localPosition) {thePos = ThisPos. pos1;} if (transform. localPosition = tran2.localPosition) {thePos = ThisPos. pos2;} if (transform. localPosition = tran3.localPosition) {thePos = ThisPos. pos3;} if (transform. localPosition = tran4.localPosition) {thePos = ThisPos. pos4 ;}// change the position, depth, and transparency of the UI. void OnTweenUI () {if (isLeft) {if (thePos = ThisPos. pos0) {// Change UISprite TweenAlpha to 0.6f and UISprite. depth to 0 AlphaChange (0.85f, 0.6f, 0); // Change TweenTransform to front-leftTransformChange (transform, tran4);} if (thePos = ThisPos. pos1) {// Change UISprite TweenAlpha to 0.85f and UISprite. depth to 4 AlphaChange (0.6f, 0.85f, 4); // Change TweenTransform to forwardTransformChange (transform, tran0);} if (thePos = ThisPos. pos2) {// Change UISprite TweenAlpha to 0.6f and UISprite. depth to 0 AlphaChange (0.4f, 0.6f, 0); // Change TweenTransform to front-rightTransformChange (transform, tran1);} if (thePos = ThisPos. pos3) {// Change UISprite TweenAlpha to 0.4f and UISprite. depth to-1 AlphaChange (0.4f, 0.4f,-1); // Change TweenTransform to back-rightTransformChange (transform, tran2);} if (thePos = ThisPos. pos4) {// Change UISprite TweenAlpha to 0.4f and UISprite. depth to-1 AlphaChange (0.6f, 0.4f,-1); // Change TweenTransform to back-leftTransformChange (transform, tran3);} isLeft = false;} if (isRight) {if (thePos = ThisPos. pos0) {// Change UISprite TweenAlpha to 0.6f and UISprite. depth to 0 AlphaChange (0.85f, 0.6f, 0); // Change TweenTransform to front-rightTransformChange (transform, tran1);} if (thePos = ThisPos. pos1) {// Change UISprite TweenAlpha to 0.4f and UISprite. depth to-1 AlphaChange (0.6f, 0.4f,-1); // Change TweenTransform to back-rightTransformChange (transform, tran2);} if (thePos = ThisPos. pos2) {// Change UISprite TweenAlpha to 0.4f and UISprite. depth to-1 AlphaChange (0.4f, 0.4f,-1); // Change TweenTransform to back-leftTransformChange (transform, tran3);} if (thePos = ThisPos. pos3) {// Change UISprite TweenAlpha to 0.6f and UISprite. depth to 0 AlphaChange (0.4f, 0.6f, 0); // Change TweenTransform to front-leftTransformChange (transform, tran4);} if (thePos = ThisPos. pos4) {// Change UISprite TweenAlpha to 0.85f and UISprite. depth to 4 AlphaChange (0.6f, 0.85f, 4); // Change TweenTransform to forwardTransformChange (transform, tran0);} isRight = false ;}} // used to control the transparency and depth of the UI background void AlphaChange (float alphaForm, float alphaTo, int UIDepth) {UISprite sprite = gameObject. getComponentInChildren <UISprite> (); sprite. depth = UIDepth; TweenAlpha phosphatase = TweenAlpha. begin (sprite. gameObject, duration, alphaTo. from = alphaForm;} // used to control the Transform position of the UI, including position, rotation, and scalevoid TransformChange (Transform tranformForm, Transform tranformTo) {TweenTransform tran = TweenTransform. begin (gameObject, duration, tranformTo); tran. from = tranformForm; tran. eventcycler = gameObject; tran. callWhenFinished = "completed";} // call void completed () {Destroy (GetComponentInChildren <TweenAlpha> (); Destroy (GetComponent <TweenTransform> () when the animation ends ());} // used as another class call. You can also declare the isLeft and isRight variables as publicpublic void OnChangeDarg (bool Left) {if (Left) isLeft = true; elseisRight = true ;}}

Code Class 2 is mainly used to trigger an animation class. Drag it to two buttons and rotate the UI's parent node. Remember that the parent node must add a Collider trigger to trigger the OnDrag event.

Using UnityEngine; using System. collections; // used to distinguish between the button and the parent node public enum DragButton {drag, buttonLeft, buttonRight} public class DragRotationPanel: MonoBehaviour {public DragButton ButtonState; // public Transform [] childs; // whether to touch bool isTouch = false; // whether to slide left // whether to slide bool isOnDrag = false; // Use this for initializationvoid Start () {} // Update is called once per framevoid Update () {} void OnClick () {if (! IsTouch) {// press the button on the left if (ButtonState = DragButton. buttonLeft) {RotationNGUI [] rotNGUI = gameObject. transform. parent. getComponentsInChildren <RotationNGUI> (); foreach (RotationNGUI rot in rotNGUI) {rot. onChangeDarg (true) ;}}// press the button on the right if (ButtonState = DragButton. buttonRight) {RotationNGUI [] rotNGUI = gameObject. transform. parent. getComponentsInChildren <RotationNGUI> (); foreach (RotationNGUI rot in rotNGUI) {rot. OnChangeDarg (false) ;}} isTouch = true; StartCoroutine (waitForNextTouch () ;}} void OnDrag (Vector2 delta) {if (ButtonState = DragButton. drag) {// to avoid event conflicts // only one sliding event if (! IsTouch) {if (delta. x> 0.2f) {// slide to the left // isRight = true; // isOnDrag = true; RotationNGUI [] rotNGUI = gameObject. transform. parent. getComponentsInChildren <RotationNGUI> (); foreach (RotationNGUI rot in rotNGUI) {rot. onChangeDarg (false) ;}} else if (delta. x <-0.2f) {// slide to the right // isLeft = true; // isOnDrag = true; RotationNGUI [] rotNGUI = GetComponentsInChildren <RotationNGUI> (); foreach (RotationNGUI rot in rotNGUI) {rot. onChangeDarg (true) ;}} isTouch = true; StartCoroutine (waitForNextTouch () ;}}// collaborate. The next operation can be performed only after the Tween ends, avoid Tween errors IEnumerator waitForNextTouch () {yield return new WaitForSeconds (0.7f); isTouch = false ;}}

Finally, the UI level you have finished should be like this, such

OK. The end is perfect!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.