CreateInstance
How do I use Create Instance
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonEditor.cs
Copy
827 {
828 get
829 {
830 if (currentSettings == null)
831 {
832 // find out if ServerSettings can be instantiated (existing script check)
833 ScriptableObject serverSettingTest = CreateInstance("ServerSettings");
834 if (serverSettingTest == null)
835 {
836 Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
837 return null;
838 }
839 DestroyImmediate(serverSettingTest);
840
841 // try to load settings from file
842 ReLoadCurrentSettings();
843
844 // if still not loaded, create one
845 if (currentSettings == null)
846 {
847 string settingsPath = Path.GetDirectoryName(PhotonNetwork.serverSettingsAssetPath);
848 if (!Directory.Exists(settingsPath))
849 {
850 Directory.CreateDirectory(settingsPath);
851 AssetDatabase.ImportAsset(settingsPath);
852 }
853
854 currentSettings = (ServerSettings)ScriptableObject.CreateInstance("ServerSettings");
855 if (currentSettings != null)
856 {
857 AssetDatabase.CreateAsset(currentSettings, PhotonNetwork.serverSettingsAssetPath);
858 }
859 else
860 {
861 Debug.LogError(CurrentLang.ServerSettingsMissingLabel);
862 }
863 }
864
865 // settings were loaded or created. set this editor's initial selected region now (will be changed in GUI)
866 if (currentSettings != null)
867 {
868 selectedRegion = currentSettings.PreferredRegion;
869 }
870 }
871
872 return currentSettings;
873 }
874
875 protected set
876 {
877 currentSettings = value;
878 }
879 }
File name: PunSceneSettings.cs
Copy
50 {
51 get
52 {
53 if (instanceField != null)
54 {
55 return instanceField;
56 }
57
58 instanceField = (PunSceneSettings)AssetDatabase.LoadAssetAtPath(PunSceneSettingsCsPath, typeof(PunSceneSettings));
59 if (instanceField == null)
60 {
61 instanceField = ScriptableObject.CreateInstance
62 AssetDatabase.CreateAsset(instanceField, PunSceneSettingsCsPath);
63 }
64
65 return instanceField;
66 }
67 }
File name: PingCloudRegions.cs
Copy
118 public IEnumerator PingSocket(Region region)
119 {
120 region.Ping = Attempts*MaxMilliseconsPerPing;
121
122 this.PingsRunning++; // TODO: Add try-catch to make sure the PingsRunning are reduced at the end and that the lib does not crash the app
123 PhotonPing ping;
124 //Debug.Log("PhotonHandler.PingImplementation " + PhotonHandler.PingImplementation);
125 if (PhotonHandler.PingImplementation == typeof(PingNativeDynamic))
126 {
127 Debug.Log("Using constructor for new PingNativeDynamic()"); // it seems on android, the Activator can't find the default Constructor
128 ping = new PingNativeDynamic();
129 }
130 else
131 {
132 ping = (PhotonPing)Activator.CreateInstance(PhotonHandler.PingImplementation);
133 }
134
135 //Debug.Log("Ping is: " + ping + " type " + ping.GetType());
136
137 float rttSum = 0.0f;
138 int replyCount = 0;
139
140
141 // PhotonPing.StartPing() requires a plain IP address without port (on all but Windows 8 platforms).
142 // So: remove port and do the DNS-resolving if needed
143 string cleanIpOfRegion = region.HostAndPort;
144 int indexOfColon = cleanIpOfRegion.LastIndexOf(':');
145 if (indexOfColon > 1)
146 {
147 cleanIpOfRegion = cleanIpOfRegion.Substring(0, indexOfColon);
148 }
149 cleanIpOfRegion = ResolveHost(cleanIpOfRegion);
150 //Debug.Log("Resolved and port-less IP is: " + cleanIpOfRegion);
151
152
153 for (int i = 0; i < Attempts; i++)
154 {
155 bool overtime = false;
156 Stopwatch sw = new Stopwatch();
157 sw.Start();
158
159 try
160 {
161 ping.StartPing(cleanIpOfRegion);
162 }
163 catch (Exception e)
164 {
165 Debug.Log("catched: " + e);
166 this.PingsRunning--;
167 break;
168 }
169
170
171 while (!ping.Done())
172 {
173 if (sw.ElapsedMilliseconds >= MaxMilliseconsPerPing)
174 {
175 overtime = true;
176 break;
177 }
178 yield return 0; // keep this loop tight, to avoid adding local lag to rtt.
179 }
180 int rtt = (int)sw.ElapsedMilliseconds;
181
182
183 if (IgnoreInitialAttempt && i == 0)
184 {
185 // do nothing.
186 }
187 else if (ping.Successful && !overtime)
188 {
189 rttSum += rtt;
190 replyCount++;
191 region.Ping = (int)((rttSum) / replyCount);
192 //Debug.Log("region " + region.Code + " RTT " + region.Ping + " success: " + ping.Successful + " over: " + overtime);
193 }
194
195 yield return new WaitForSeconds(0.1f);
196 }
197
198 this.PingsRunning--;
199
200 //Debug.Log("this.PingsRunning: " + this.PingsRunning + " this debug: " + ping.DebugString);
201 yield return null;
202 }
File name: GameController.cs
Copy
18 void Awake(){
19 CreateInstance ();
20 InitializeGameVariables ();
21 }
File name: GameController.cs
Copy
33 void CreateInstance(){
34 if (instance != null) {
35 Destroy (gameObject);
36 } else {
37 instance = this;
38 DontDestroyOnLoad (gameObject);
39 }
40 }
File name: GameplayController.cs
Copy
119 void CreateInstance(){
120 if(instance == null){
121 instance = this;
122 }
123 }
File name: MusicController.cs
Copy
13 void Awake(){
14 audioSource = GetComponent
15 CreateInstance ();
16 }
File name: MusicController.cs
Copy
23 void CreateInstance(){
24 if (instance != null) {
25 Destroy (gameObject);
26 } else {
27 instance = this;
28 DontDestroyOnLoad (gameObject);
29 }
30 }
CreateInstance 81 lượt xem
Gõ tìm kiếm nhanh...