UnityEngine
How do I use Unity Engine
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonEditor.cs
Copy
886 public static void ReLoadCurrentSettings()
887 {
888 // this now warns developers if there are more than one settings files in resources folders. first will be used.
889 UnityEngine.Object[] settingFiles = Resources.LoadAll(PhotonNetwork.serverSettingsAssetFile, typeof(ServerSettings));
890 if (settingFiles != null && settingFiles.Length > 0)
891 {
892 PhotonEditor.Current = (ServerSettings)settingFiles[0];
893
894 if (settingFiles.Length > 1)
895 {
896 Debug.LogWarning(CurrentLang.MoreThanOneLabel + PhotonNetwork.serverSettingsAssetFile + CurrentLang.FilesInResourceFolderLabel + AssetDatabase.GetAssetPath(PhotonEditor.Current));
897 }
898 }
899 }
File name: PhotonEditor.cs
Copy
909 public static void UpdateRpcList()
910 {
911 List
912 HashSet
913
914 var types = GetAllSubTypesInScripts(typeof(MonoBehaviour));
915
916 foreach (var mono in types)
917 {
918 MethodInfo[] methods = mono.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
919
920 foreach (MethodInfo method in methods)
921 {
922 if (method.IsDefined(typeof(UnityEngine.RPC), false))
923 {
924 currentRpcs.Add(method.Name);
925
926 if (!additionalRpcs.Contains(method.Name) && !PhotonEditor.Current.RpcList.Contains(method.Name))
927 {
928 additionalRpcs.Add(method.Name);
929 }
930 }
931 }
932 }
933
934 if (additionalRpcs.Count > 0)
935 {
936 // LIMITS RPC COUNT
937 if (additionalRpcs.Count + PhotonEditor.Current.RpcList.Count >= byte.MaxValue)
938 {
939 if (currentRpcs.Count <= byte.MaxValue)
940 {
941 bool clearList = EditorUtility.DisplayDialog(CurrentLang.IncorrectRPCListTitle, CurrentLang.IncorrectRPCListLabel, CurrentLang.RemoveOutdatedRPCsLabel, CurrentLang.CancelButton);
942 if (clearList)
943 {
944 PhotonEditor.Current.RpcList.Clear();
945 PhotonEditor.Current.RpcList.AddRange(currentRpcs);
946 }
947 else
948 {
949 return;
950 }
951 }
952 else
953 {
954 EditorUtility.DisplayDialog(CurrentLang.FullRPCListTitle, CurrentLang.FullRPCListLabel, CurrentLang.SkipRPCListUpdateLabel);
955 return;
956 }
957 }
958
959 additionalRpcs.Sort();
960 PhotonEditor.Current.RpcList.AddRange(additionalRpcs);
961 EditorUtility.SetDirty(PhotonEditor.Current);
962 }
963 }
File name: Extensions.cs
Copy
22 public static PhotonView[] GetPhotonViewsInChildren(this UnityEngine.GameObject go)
23 {
24 return go.GetComponentsInChildren
25 }
File name: Extensions.cs
Copy
27 public static PhotonView GetPhotonView(this UnityEngine.GameObject go)
28 {
29 return go.GetComponent
30 }
File name: NetworkingPeer.cs
Copy
2567 public int GetInstantiatedObjectsId(GameObject go)
2568 {
2569 int id = -1;
2570 if (go == null)
2571 {
2572 Debug.LogError("GetInstantiatedObjectsId() for GO == null.");
2573 return id;
2574 }
2575
2576 PhotonView[] pvs = go.GetPhotonViewsInChildren();
2577 if (pvs != null && pvs.Length > 0 && pvs[0] != null)
2578 {
2579 return pvs[0].instantiationId;
2580 }
2581
2582 if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
2583 UnityEngine.Debug.Log("GetInstantiatedObjectsId failed for GO: " + go);
2584
2585
2586 return id;
2587 }
File name: PhotonHandler.cs
Copy
59 protected void Update()
60 {
61 if (PhotonNetwork.networkingPeer == null)
62 {
63 Debug.LogError("NetworkPeer broke!");
64 return;
65 }
66
67 if (PhotonNetwork.connectionStateDetailed == PeerState.PeerCreated || PhotonNetwork.connectionStateDetailed == PeerState.Disconnected || PhotonNetwork.offlineMode)
68 {
69 return;
70 }
71
72 // the messageQueue might be paused. in that case a thread will send acknowledgements only. nothing else to do here.
73 if (!PhotonNetwork.isMessageQueueRunning)
74 {
75 return;
76 }
77
78 bool doDispatch = true;
79 while (PhotonNetwork.isMessageQueueRunning && doDispatch)
80 {
81 // DispatchIncomingCommands() returns true of it found any command to dispatch (event, result or state change)
82 UnityEngine.Profiling.Profiler.BeginSample("DispatchIncomingCommands");
83 doDispatch = PhotonNetwork.networkingPeer.DispatchIncomingCommands();
84 UnityEngine.Profiling.Profiler.EndSample();
85 }
86
87 int currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000); // avoiding Environment.TickCount, which could be negative on long-running platforms
88 if (PhotonNetwork.isMessageQueueRunning && currentMsSinceStart > this.nextSendTickCountOnSerialize)
89 {
90 PhotonNetwork.networkingPeer.RunViewUpdate();
91 this.nextSendTickCountOnSerialize = currentMsSinceStart + this.updateIntervalOnSerialize;
92 this.nextSendTickCount = 0; // immediately send when synchronization code was running
93 }
94
95 currentMsSinceStart = (int)(Time.realtimeSinceStartup * 1000);
96 if (currentMsSinceStart > this.nextSendTickCount)
97 {
98 bool doSend = true;
99 while (PhotonNetwork.isMessageQueueRunning && doSend)
100 {
101 // Send all outgoing commands
102 UnityEngine.Profiling.Profiler.BeginSample("SendOutgoingCommands");
103 doSend = PhotonNetwork.networkingPeer.SendOutgoingCommands();
104 UnityEngine.Profiling.Profiler.EndSample();
105 }
106
107 this.nextSendTickCount = currentMsSinceStart + this.updateInterval;
108 }
109 }
File name: Room.cs
Copy
59 {
60 get
61 {
62 return (int)this.maxPlayersField;
63 }
64
65 set
66 {
67 if (!this.Equals(PhotonNetwork.room))
68 {
69 UnityEngine.Debug.LogWarning("Can't set maxPlayers when not in that room.");
70 }
71
72 if (value > 255)
73 {
74 UnityEngine.Debug.LogWarning("Can't set Room.maxPlayers to: " + value + ". Using max value: 255.");
75 value = 255;
76 }
77
78 if (value != this.maxPlayersField && !PhotonNetwork.offlineMode)
79 {
80 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.MaxPlayers, (byte)value } }, true, (byte)0);
81 }
82
83 this.maxPlayersField = (byte)value;
84 }
85 }
File name: Room.cs
Copy
96 {
97 get
98 {
99 return this.openField;
100 }
101
102 set
103 {
104 if (!this.Equals(PhotonNetwork.room))
105 {
106 UnityEngine.Debug.LogWarning("Can't set open when not in that room.");
107 }
108
109 if (value != this.openField && !PhotonNetwork.offlineMode)
110 {
111 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.IsOpen, value } }, true, (byte)0);
112 }
113
114 this.openField = value;
115 }
116 }
File name: Room.cs
Copy
124 {
125 get
126 {
127 return this.visibleField;
128 }
129
130 set
131 {
132 if (!this.Equals(PhotonNetwork.room))
133 {
134 UnityEngine.Debug.LogWarning("Can't set visible when not in that room.");
135 }
136
137 if (value != this.visibleField && !PhotonNetwork.offlineMode)
138 {
139 PhotonNetwork.networkingPeer.OpSetPropertiesOfRoom(new Hashtable() { { GameProperties.IsVisible, value } }, true, (byte)0);
140 }
141
142 this.visibleField = value;
143 }
144 }
File name: ChatClient.cs
Copy
94 public bool Connect(string address, ConnectionProtocol protocol, string appId, string appVersion, string userId, AuthenticationValues authValues)
95 {
96 if (!this.HasPeer)
97 {
98 this.chatPeer = new ChatPeer(this, protocol);
99 }
100 else
101 {
102 this.Disconnect();
103 if (this.chatPeer.UsedProtocol != protocol)
104 {
105 this.chatPeer = new ChatPeer(this, protocol);
106 }
107 }
108
109#if UNITY
110#pragma warning disable 0162 // the library variant defines if we should use PUN's SocketUdp variant (at all)
111 if (PhotonPeer.NoSocket)
112 {
113#if !UNITY_EDITOR && (UNITY_PS3 || UNITY_ANDROID)
114 UnityEngine.Debug.Log("Using class SocketUdpNativeDynamic");
115 this.chatPeer.SocketImplementation = typeof(SocketUdpNativeDynamic);
116#elif !UNITY_EDITOR && UNITY_IPHONE
117 UnityEngine.Debug.Log("Using class SocketUdpNativeStatic");
118 this.chatPeer.SocketImplementation = typeof(SocketUdpNativeStatic);
119#elif !UNITY_EDITOR && (UNITY_WINRT)
120 // this automatically uses a separate assembly-file with Win8-style Socket usage (not possible in Editor)
121#else
122 Type udpSocket = Type.GetType("ExitGames.Client.Photon.SocketUdp, Assembly-CSharp");
123 this.chatPeer.SocketImplementation = udpSocket;
124 if (udpSocket == null)
125 {
126 UnityEngine.Debug.Log("ChatClient could not find a suitable C# socket class. The Photon3Unity3D.dll only supports native socket plugins.");
127 }
128#endif
129 if (this.chatPeer.SocketImplementation == null)
130 {
131 UnityEngine.Debug.Log("No socket implementation set for 'NoSocket' assembly. Please contact Exit Games.");
132 }
133 }
134#pragma warning restore 0162
135#endif
136
137 this.chatPeer.TimePingInterval = 3000;
138 this.DisconnectedCause = ChatDisconnectCause.None;
139
140 this.CustomAuthenticationValues = authValues;
141 this.UserId = userId;
142 this.AppId = appId;
143 this.AppVersion = appVersion;
144 this.didAuthenticate = false;
145 this.msDeltaForServiceCalls = 100;
146
147
148 // clean all channels
149 this.PublicChannels.Clear();
150 this.PrivateChannels.Clear();
151
152 if (!address.Contains(":"))
153 {
154 int port = 0;
155 ProtocolToNameServerPort.TryGetValue(protocol, out port);
156 address = string.Format("{0}:{1}", address, port);
157 }
158
159 bool isConnecting = this.chatPeer.Connect(address, "NameServer");
160 if (isConnecting)
161 {
162 this.State = ChatState.ConnectingToNameServer;
163 }
164 return isConnecting;
165 }
Download file with original file name:UnityEngine
UnityEngine 126 lượt xem
Gõ tìm kiếm nhanh...