Action









How do I use Action
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: CubeLerp.cs Copy
35     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
36     {
37         if (stream.isWriting)
38         {
39             Vector3 pos = transform.localPosition;
40             Quaternion rot = transform.localRotation;
41             stream.Serialize(ref pos);
42             stream.Serialize(ref rot);
43         }
44         else
45         {
46             // Receive latest state information
47             Vector3 pos = Vector3.zero;
48             Quaternion rot = Quaternion.identity;
49
50             stream.Serialize(ref pos);
51             stream.Serialize(ref rot);
52
53             latestCorrectPos = pos; // save this to move towards it in FixedUpdate()
54             onUpdatePos = transform.localPosition; // we interpolate from here to latestCorrectPos
55             fraction = 0; // reset the fraction we alreay moved. see Update()
56
57             transform.localRotation = rot; // this sample doesn't smooth rotation
58         }
59     }
File name: CubeLerp.cs Copy
61     public void Update()
62     {
63         // We get 10 updates per sec. sometimes a few less or one or two more, depending on variation of lag.
64         // Due to that we want to reach the correct position in a little over 100ms. This way, we usually avoid a stop.
65         // Lerp() gets a fraction value between 0 and 1. This is how far we went from A to B.
66         //
67         // Our fraction variable would reach 1 in 100ms if we multiply deltaTime by 10.
68         // We want it to take a bit longer, so we multiply with 9 instead.
69
70         fraction = fraction + Time.deltaTime * 9;
71         transform.localPosition = Vector3.Lerp(onUpdatePos, latestCorrectPos, fraction); // set our pos between A and B
72     }
File name: AccountService.cs Copy
27     private Action registrationCallback; // optional (when using async reg)
File name: AccountService.cs Copy
93     public void RegisterByEmailAsync(string email, Origin origin, Action callback = null)
94     {
95         this.registrationCallback = callback;
96         this.AppId = string.Empty;
97         this.Message = string.Empty;
98         this.ReturnCode = -1;
99
100         try
101         {
102             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
103             req.Timeout = 5000;
104             req.BeginGetResponse(this.OnRegisterByEmailCompleted, req);
105         }
106         catch (Exception ex)
107         {
108             this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
109             this.Exception = ex;
110             if (this.registrationCallback != null)
111             {
112                 this.registrationCallback(this);
113             }
114         }
115     }
File name: PhotonEditor.cs Copy
318     private static void OnUpdate()
319     {
320         // after a compile, check RPCs to create a cache-list
321         if (!postCompileActionsDone && !EditorApplication.isCompiling && !EditorApplication.isPlayingOrWillChangePlaymode && PhotonEditor.Current != null)
322         {
323             #if UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_1 || UNITY_5_2
324             if (EditorApplication.isUpdating) return;
325             #endif
326
327             PhotonEditor.UpdateRpcList();
328             postCompileActionsDone = true; // on compile, this falls back to false (without actively doing anything)
329
330             #if UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_5_1 || UNITY_5_2
331             PhotonEditor.ImportWin8Support();
332             #endif
333         }
334     }
File name: Signals.cs Copy
7         private event Action Listener = delegate { };
File name: Signals.cs Copy
8         private event Action OnceListener = delegate { };
File name: Signals.cs Copy
10         public void AddListener(Action callback)
11         {
12             Listener += callback;
13         }
File name: Signals.cs Copy
15         public void AddOnce(Action callback)
16         {
17             OnceListener += callback;
18         }
File name: Signals.cs Copy
20         public void RemoveListener(Action callback)
21         {
22             Listener -= callback;
23         }

Download file with original file name:Action

Action 127 lượt xem

Gõ tìm kiếm nhanh...