ToArray









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

Featured Snippets


File name: ChatGui.cs Copy
103     public void OnGUI()
104     {
105         if (!this.IsVisible)
106         {
107             return;
108         }
109
110         GUI.skin.label.wordWrap = true;
111         //GUI.skin.button.richText = true; // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
112         //GUILayout.Button("lala"); // as richText, html tags could be in text
113
114
115         if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
116         {
117             if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
118             {
119                 // focus on input -> submit it
120                 GuiSendsMsg();
121                 return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
122             }
123             else
124             {
125                 // assign focus to input
126                 GUI.FocusControl("ChatInput");
127             }
128         }
129
130         GUI.SetNextControlName("");
131         GUILayout.BeginArea(this.GuiRect);
132
133         GUILayout.FlexibleSpace();
134
135         if (this.chatClient.State != ChatState.ConnectedToFrontEnd)
136         {
137             GUILayout.Label("Not in chat yet.");
138         }
139         else
140         {
141             List channels = new List(this.chatClient.PublicChannels.Keys); // this could be cached
142             int countOfPublicChannels = channels.Count;
143             channels.AddRange(this.chatClient.PrivateChannels.Keys);
144
145             if (channels.Count > 0)
146             {
147                 int previouslySelectedChannelIndex = this.selectedChannelIndex;
148                 int channelIndex = channels.IndexOf(this.selectedChannelName);
149                 this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;
150
151                 this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
152                 this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);
153
154                 this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
155                 this.selectedChannelName = channels[this.selectedChannelIndex];
156
157                 if (this.selectedChannelIndex != previouslySelectedChannelIndex)
158                 {
159                     // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
160                     this.scrollPos.y = float.MaxValue;
161                     if (this.doingPrivateChat)
162                     {
163                         string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
164                         this.userIdInput = pieces[1];
165                     }
166                 }
167
168                 GUILayout.Label(ChatGui.WelcomeText);
169
170                 if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
171                 {
172                     for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
173                     {
174                         string sender = this.selectedChannel.Senders[i];
175                         object message = this.selectedChannel.Messages[i];
176                         GUILayout.Label(string.Format("{0}: {1}", sender, message));
177                     }
178                 }
179
180                 GUILayout.EndScrollView();
181             }
182         }
183
184
185         GUILayout.BeginHorizontal();
186         if (doingPrivateChat)
187         {
188             GUILayout.Label("to:", GUILayout.ExpandWidth(false));
189             GUI.SetNextControlName("WhisperTo");
190             this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
191             string focussed = GUI.GetNameOfFocusedControl();
192             if (focussed.Equals("WhisperTo"))
193             {
194                 if (this.userIdInput.Equals("username"))
195                 {
196                     this.userIdInput = "";
197                 }
198             }
199             else if (string.IsNullOrEmpty(this.userIdInput))
200             {
201                 this.userIdInput = "username";
202             }
203
204         }
205         GUI.SetNextControlName("ChatInput");
206         inputLine = GUILayout.TextField(inputLine);
207         if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
208         {
209             GuiSendsMsg();
210         }
211         GUILayout.EndHorizontal();
212         GUILayout.EndArea();
213     }
File name: PunStartup.cs Copy
80     public static void SetPunDemoBuildSettings()
81     {
82         // find path of pun guide
83         string[] tempPaths = Directory.GetDirectories(Application.dataPath + "/Photon Unity Networking", "Demos", SearchOption.AllDirectories);
84         if (tempPaths == null || tempPaths.Length != 1)
85         {
86             return;
87         }
88
89         // find scenes of guide
90         string guidePath = tempPaths[0];
91         tempPaths = Directory.GetFiles(guidePath, "*.unity", SearchOption.AllDirectories);
92
93         if (tempPaths == null || tempPaths.Length == 0)
94         {
95             return;
96         }
97
98         // add found guide scenes to build settings
99         List sceneAr = new List();
100         for (int i = 0; i < tempPaths.Length; i++)
101         {
102             //Debug.Log(tempPaths[i]);
103             string path = tempPaths[i].Substring(Application.dataPath.Length - "Assets".Length);
104             path = path.Replace('\\', '/');
105             //Debug.Log(path);
106
107             if (path.Contains("PUNGuide_M2H"))
108             {
109                 continue;
110             }
111
112             if (path.Contains("Hub"))
113             {
114                 sceneAr.Insert(0, new EditorBuildSettingsScene(path, true));
115                 continue;
116             }
117
118             sceneAr.Add(new EditorBuildSettingsScene(path, true));
119         }
120
121         EditorBuildSettings.scenes = sceneAr.ToArray();
122         EditorApplication.OpenScene(sceneAr[0].path);
123     }
File name: PhotonEditor.cs Copy
975     public static System.Type[] GetAllSubTypesInScripts(System.Type aBaseClass)
976     {
977         var result = new System.Collections.Generic.List();
978         System.Reflection.Assembly[] AS = System.AppDomain.CurrentDomain.GetAssemblies();
979         foreach (var A in AS)
980         {
981             // this skips all but the Unity-scripted assemblies for RPC-list creation. You could remove this to search all assemblies in project
982             if (!A.FullName.StartsWith("Assembly-"))
983             {
984                 // Debug.Log("Skipping Assembly: " + A);
985                 continue;
986             }
987
988             //Debug.Log("Assembly: " + A.FullName);
989             System.Type[] types = A.GetTypes();
990             foreach (var T in types)
991             {
992                 if (T.IsSubclassOf(aBaseClass))
993                     result.Add(T);
994             }
995         }
996         return result.ToArray();
997     }
File name: NetworkingPeer.cs Copy
616     void RebuildPlayerListCopies()
617     {
618         this.mPlayerListCopy = new PhotonPlayer[this.mActors.Count];
619         this.mActors.Values.CopyTo(this.mPlayerListCopy, 0);
620
621         List otherP = new List();
622         foreach (PhotonPlayer player in this.mPlayerListCopy)
623         {
624             if (!player.isLocal)
625             {
626                 otherP.Add(player);
627             }
628         }
629
630         this.mOtherPlayerListCopy = otherP.ToArray();
631     }
File name: NetworkingPeer.cs Copy
1918     private void SendVacantViewIds()
1919     {
1920         Debug.Log("SendVacantViewIds()");
1921         List vacantViews = new List();
1922         foreach (PhotonView view in this.photonViewList.Values)
1923         {
1924             if (!view.isOwnerActive)
1925             {
1926                 vacantViews.Add(view.viewID);
1927             }
1928         }
1929
1930         Debug.Log("Sending vacant view IDs. Length: " + vacantViews.Count);
1931         //this.OpRaiseEvent(PunEvent.VacantViewIds, true, vacantViews.ToArray());
1932         this.OpRaiseEvent(PunEvent.VacantViewIds, vacantViews.ToArray(), true, null);
1933     }
File name: NetworkingPeer.cs Copy
3034     public void SetReceivingEnabled(int[] enableGroups, int[] disableGroups)
3035     {
3036         List enableList = new List();
3037         List disableList = new List();
3038
3039         if (enableGroups != null)
3040         {
3041             for (int index = 0; index < enableGroups.Length; index++)
3042             {
3043                 int i = enableGroups[index];
3044                 if (i <= 0)
3045                 {
3046                     Debug.LogError("Error: PhotonNetwork.SetReceivingEnabled was called with an illegal group number: " + i + ". The group number should be at least 1.");
3047                     continue;
3048                 }
3049                 if (!this.allowedReceivingGroups.Contains(i))
3050                 {
3051                     this.allowedReceivingGroups.Add(i);
3052                     enableList.Add((byte)i);
3053                 }
3054             }
3055         }
3056         if (disableGroups != null)
3057         {
3058             for (int index = 0; index < disableGroups.Length; index++)
3059             {
3060                 int i = disableGroups[index];
3061                 if (i <= 0)
3062                 {
3063                     Debug.LogError("Error: PhotonNetwork.SetReceivingEnabled was called with an illegal group number: " + i + ". The group number should be at least 1.");
3064                     continue;
3065                 }
3066                 if (enableList.Contains((byte)i))
3067                 {
3068                     Debug.LogError("Error: PhotonNetwork.SetReceivingEnabled disableGroups contains a group that is also in the enableGroups: " + i + ".");
3069                     continue;
3070                 }
3071                 if (this.allowedReceivingGroups.Contains(i))
3072                 {
3073                     this.allowedReceivingGroups.Remove(i);
3074                     disableList.Add((byte)i);
3075                 }
3076             }
3077         }
3078
3079         this.OpChangeGroups(disableList.Count > 0 ? disableList.ToArray() : null, enableList.Count > 0 ? enableList.ToArray() : null); //Passing a 0 sized array != passing null
3080     }
File name: NetworkingPeer.cs Copy
3271     // calls OnPhotonSerializeView (through ExecuteOnSerialize)
3273     private Hashtable OnSerializeWrite(PhotonView view)
3274     {
3275         PhotonStream pStream = new PhotonStream( true, null );
3276         PhotonMessageInfo info = new PhotonMessageInfo( this.mLocalActor, this.ServerTimeInMilliSeconds, view );
3277
3278         // each view creates a list of values that should be sent
3279         view.SerializeView( pStream, info );
3280
3281         if( pStream.Count == 0 )
3282         {
3283             return null;
3284         }
3285
3286         object[] dataArray = pStream.data.ToArray();
3287
3288         if (view.synchronization == ViewSynchronization.UnreliableOnChange)
3289         {
3290             if (AlmostEquals(dataArray, view.lastOnSerializeDataSent))
3291             {
3292                 if (view.mixedModeIsReliable)
3293                 {
3294                     return null;
3295                 }
3296
3297                 view.mixedModeIsReliable = true;
3298                 view.lastOnSerializeDataSent = dataArray;
3299             }
3300             else
3301             {
3302                 view.mixedModeIsReliable = false;
3303                 view.lastOnSerializeDataSent = dataArray;
3304             }
3305         }
3306
3307         // EVDATA:
3308         // 0=View ID (an int, never compressed cause it's not in the data)
3309         // 1=data of observed type (different per type of observed object)
3310         // 2=compressed data (in this case, key 1 is empty)
3311         // 3=list of values that are actually null (if something was changed but actually IS null)
3312         Hashtable evData = new Hashtable();
3313         evData[(byte)0] = (int)view.viewID;
3314         evData[(byte)1] = dataArray; // this is the actual data (script or observed object)
3315
3316
3317         if (view.synchronization == ViewSynchronization.ReliableDeltaCompressed)
3318         {
3319             // compress content of data set (by comparing to view.lastOnSerializeDataSent)
3320             // the "original" dataArray is NOT modified by DeltaCompressionWrite
3321             // if something was compressed, the evData key 2 and 3 are used (see above)
3322             bool somethingLeftToSend = this.DeltaCompressionWrite(view, evData);
3323
3324             // buffer the full data set (for next compression)
3325             view.lastOnSerializeDataSent = dataArray;
3326
3327             if (!somethingLeftToSend)
3328             {
3329                 return null;
3330             }
3331         }
3332
3333         return evData;
3334     }
File name: NetworkingPeer.cs Copy
3425     private bool DeltaCompressionWrite(PhotonView view, Hashtable data)
3426     {
3427         if (view.lastOnSerializeDataSent == null)
3428         {
3429             return true; // all has to be sent
3430         }
3431
3432         // We can compress as we sent a full update previously (readers can re-use previous values)
3433         object[] lastData = view.lastOnSerializeDataSent;
3434         object[] currentContent = data[(byte)1] as object[];
3435
3436         if (currentContent == null)
3437         {
3438             // no data to be sent
3439             return false;
3440         }
3441
3442         if (lastData.Length != currentContent.Length)
3443         {
3444             // if new data isn't same length as before, we send the complete data-set uncompressed
3445             return true;
3446         }
3447
3448         object[] compressedContent = new object[currentContent.Length];
3449         int compressedValues = 0;
3450
3451         List valuesThatAreChangedToNull = new List();
3452         for (int index = 0; index < compressedContent.Length; index++)
3453         {
3454             object newObj = currentContent[index];
3455             object oldObj = lastData[index];
3456             if (this.ObjectIsSameWithInprecision(newObj, oldObj))
3457             {
3458                 // compress (by using null, instead of value, which is same as before)
3459                 compressedValues++;
3460                 // compressedContent[index] is already null (initialized)
3461             }
3462             else
3463             {
3464                 compressedContent[index] = currentContent[index];
3465
3466                 // value changed, we don't replace it with null
3467                 // new value is null (like a compressed value): we have to mark it so it STAYS null instead of being replaced with previous value
3468                 if (newObj == null)
3469                 {
3470                     valuesThatAreChangedToNull.Add(index);
3471                 }
3472             }
3473         }
3474
3475         // Only send the list of compressed fields if we actually compressed 1 or more fields.
3476         if (compressedValues > 0)
3477         {
3478             data.Remove((byte)1); // remove the original data (we only send compressed data)
3479
3480             if (compressedValues == currentContent.Length)
3481             {
3482                 // all values are compressed to null, we have nothing to send
3483                 return false;
3484             }
3485
3486             data[(byte)2] = compressedContent; // current, compressted data is moved to key 2 to mark it as compressed
3487             if (valuesThatAreChangedToNull.Count > 0)
3488             {
3489                 data[(byte)3] = valuesThatAreChangedToNull.ToArray(); // data that is actually null (not just cause we didn't want to send it)
3490             }
3491         }
3492
3493         return true; // some data was compressed but we need to send something
3494     }
File name: PickupItemSyncer.cs Copy
87     private void SendPickedUpItems(PhotonPlayer targtePlayer)
88     {
89         if (targtePlayer == null)
90         {
91             Debug.LogWarning("Cant send PickupItem spawn times to unknown targetPlayer.");
92             return;
93         }
94
95         double now = PhotonNetwork.time;
96         double soon = now + TimeDeltaToIgnore;
97
98
99         PickupItem[] items = new PickupItem[PickupItem.DisabledPickupItems.Count];
100         PickupItem.DisabledPickupItems.CopyTo(items);
101
102         List valuesToSend = new List(items.Length * 2);
103         for (int i = 0; i < items.Length; i++)
104         {
105             PickupItem pi = items[i];
106             if (pi.SecondsBeforeRespawn <= 0)
107             {
108                 valuesToSend.Add(pi.ViewID);
109                 valuesToSend.Add((float)0.0f);
110             }
111             else
112             {
113                 double timeUntilRespawn = pi.TimeOfRespawn - PhotonNetwork.time;
114                 if (pi.TimeOfRespawn > soon)
115                 {
116                     // the respawn of this item is not "immediately", so we include it in the message "these items are not active" for the new player
117                     Debug.Log(pi.ViewID + " respawn: " + pi.TimeOfRespawn + " timeUntilRespawn: " + timeUntilRespawn + " (now: " + PhotonNetwork.time + ")");
118                     valuesToSend.Add(pi.ViewID);
119                     valuesToSend.Add((float)timeUntilRespawn);
120                 }
121             }
122         }
123
124         Debug.Log("Sent count: " + valuesToSend.Count + " now: " + now);
125         this.photonView.RPC("PickupItemInit", targtePlayer, PhotonNetwork.time, valuesToSend.ToArray());
126     }
File name: TimelineKey.cs Copy
91         void GetCurveParams(XmlElement element)
92         {
93             //Get curve parameters using a bit of XPath, order using LINQ
94             //XPath 1.0 doesn't support regex - should match all attributes with names matching "c[0-9]+"
95             var curveParams = element.SelectNodes("@*[starts-with(name(), 'c') and string(number(substring(name(),2))) != 'NaN']")
96                 .OfType()
97                 .OrderBy(attr => attr.Name);
98
99             //Cast the values to float and convert to an array
100             CurveParams = curveParams
101                 .Select(attr => float.Parse(attr.Value))
102                 .ToArray();
103         }

ToArray 122 lượt xem

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