Sun Guangdong 2015.5.11
in this article we will make a generic type ofMODALwindow (Yes, No, Maybeso, Cancel) Where we can put the content and the actionPushto the window, this window can be used anywhere in our game, the button is pressed when the event works.
The code involved:
using Unityengine;using System.collections;public class Bringtofront:monobehaviour {void Onenable () {transform. Setaslastsibling ();}}
Using unityengine;using unityengine.ui;using unityengine.events;using system.collections;//This script would be updated In Part 2 of this 2 part Series.public class Modalpanel:monobehaviour {public Text question;public Image Iconimage;publi C button Yesbutton;public button nobutton;public button cancelbutton;public gameobject modalpanelobject;private static Modalpanel modalpanel;public static Modalpanel Instance () {if (!modalpanel) {Modalpanel = Findobjectoftype (typeof (Modal Panel) as Modalpanel;if (!modalpanel) debug.logerror ("There needs to is one active Modalpanel script on a gameobject in Y Our scene. ");} return Modalpanel;} Yes/no/cancel:a string, a Yes event, a No event and Cancel eventpublic void Choice (String question, Unityaction Yesev ENT, unityaction noEvent, unityaction cancelevent) {modalpanelobject.setactive (true); YesButton.onClick.RemoveAllListeners (); YesButton.onClick.AddListener (yesevent); YesButton.onClick.AddListener ( Closepanel); noButton.onClick.RemoveAllListeNers (); NoButton.onClick.AddListener (noEvent); NoButton.onClick.AddListener (Closepanel); CancelButton.onClick.RemoveAllListeners (); CancelButton.onClick.AddListener (CancelEvent); CancelButton.onClick.AddListener (closepanel); this.question.text = Question;this.iconimage.gameobject.setactive ( FALSE); YesButton.gameObject.SetActive (true); NoButton.gameObject.SetActive (true); CancelButton.gameObject.SetActive (TRUE);} void Closepanel () {modalpanelobject.setactive (false);}}
Using unityengine;using unityengine.ui;using System.collections;public class Displaymanager:monobehaviour {public Text displaytext;public float displaytime;public float fadetime;private IEnumerator fadealpha;private static Displaymanager displaymanager;public static Displaymanager Instance () {if (!displaymanager) {Displaymanager = Findobjectoftype (typeof (Displaymanager)) as Displaymanager;if (!displaymanager) debug.logerror ("There needs to be one Active Displaymanager script on a gameobject in your scene. "); return Displaymanager;} public void DisplayMessage (String message) {displaytext.text = message; Setalpha ();} void Setalpha () {if (Fadealpha! = null) {stopcoroutine (fadealpha);} Fadealpha = Fadealpha (); Startcoroutine (Fadealpha);} IEnumerator Fadealpha () {Color Resetcolor = Displaytext.color;resetcolor.a = 1;displaytext.color = Resetcolor;yield Retu RN New Waitforseconds (DisplayTime); while (Displaytext.color.a > 0) {color displaycolor = Displaytext.color; DISPLAYCOLOR.A-= TiMe.deltatime/fadetime;displaytext.color = Displaycolor;yield return null;} yield return null;}}
Using unityengine;using unityengine.ui;using unityengine.events;using system.collections;//This script would be updated In Part 2 of this 2 part Series.public class Testmodalwindow:monobehaviour {private Modalpanel modalpanel;private Displa Ymanager displaymanager;private unityaction myyesaction;private unityaction mynoaction;private UnityAction Mycancelaction;void Awake () {Modalpanel = Modalpanel.instance ();d Isplaymanager = Displaymanager.instance (); Myyesaction = new Unityaction (testyesfunction); mynoaction = new Unityaction (testnofunction); mycancelaction = new UnityA Ction (testcancelfunction);} Send to the Modal Panel to set up the Buttons and Functions to Callpublic void Testync () {Modalpanel.choice Want to spawn this sphere? ", Testyesfunction, Testnofunction, testcancelfunction);//Modalpanel.choice (" Would you Li Ke a poke in the eye?\nhow on with a sharp stick? ", Myyesaction, Mynoaction, mycancelaction);} These is wrapped into unityactionsvoid testyeSfunction () {displaymanager.displaymessage ("Heck yeah! Yup! ");} void Testnofunction () {displaymanager.displaymessage ("No, josé!");} void Testcancelfunction () {displaymanager.displaymessage ("I give up!");}}
Talk about something else:
Resolution & Device Independence
playersettings :
iPhone6 Plus: with 1920x1080 resolution
in theresolution at one scalethrough The concept of Muding can be Open the adapter, as16:9
obvious elements in four Corners directly Muding in The corresponding foot can be!  
The middle is in the middle.
Up and down on the corresponding up and down around.
But there are still big problems with the screen getting into hours:
the next step is to: Canvas Scaler Components
So, when the screen size changes,UIIt is no longer a simple Muding, it becomes smaller as it grows larger.
Creating A scene Selection menu scene changes to toggle sound: void onlevelwasloaded (int level) { if (level = = 2) in Monobehaviour { source.clip = level2music; Source. Play (); } } Asynchronous load scenario: Using unityengine;using system.collections;using Unityengine.ui;public class Clicktoloadasync:monobehaviour { Public Slider Loadingbar; Public Gameobject loadingimage; Private asyncoperation async; public void Clickasync (Int. level) { loadingimage.setactive (true); Startcoroutine (level) (Loadlevelwithbar); } IEnumerator loadlevelwithbar (int level) { async = Application.loadlevelasync (level); while (!async.isdone) { loadingbar.value = async.progress; yield return null;}} }
??
(30) unity4.6 Learning Ugui Chinese documents-------making a generic modal window