DestroyImmediate
How do I use Destroy Immediate
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonConverter.cs
Copy
351 static int ConvertNetworkView(NetworkView[] netViews, bool isScene)
352 {
353 for (int i = netViews.Length - 1; i >= 0; i--)
354 {
355 NetworkView netView = netViews[i];
356 PhotonView view = netView.gameObject.AddComponent
357 if (isScene)
358 {
359 //Get scene ID
360 string str = netView.viewID.ToString().Replace("SceneID: ", "");
361 int firstSpace = str.IndexOf(" ");
362 str = str.Substring(0, firstSpace);
363 int oldViewID = int.Parse(str);
364
365 view.viewID = oldViewID;
366 EditorUtility.SetDirty(view);
367 EditorUtility.SetDirty(view.gameObject);
368 }
369 view.observed = netView.observed;
370 if (netView.stateSynchronization == NetworkStateSynchronization.Unreliable)
371 {
372 view.synchronization = ViewSynchronization.Unreliable;
373 }
374 else if (netView.stateSynchronization == NetworkStateSynchronization.ReliableDeltaCompressed)
375 {
376 view.synchronization = ViewSynchronization.ReliableDeltaCompressed;
377 }
378 else
379 {
380 view.synchronization = ViewSynchronization.Off;
381 }
382 DestroyImmediate(netView, true);
383 }
384 AssetDatabase.Refresh();
385 AssetDatabase.SaveAssets();
386
387 return netViews.Length;
388 }
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: PhotonHandler.cs
Copy
24 public int updateIntervalOnSerialize; // time [ms] between consecutive RunViewUpdate calls (sending syncs, etc)
35 protected void Awake()
36 {
37 if (SP != null && SP != this && SP.gameObject != null)
38 {
39 GameObject.DestroyImmediate(SP.gameObject);
40 }
41
42 SP = this;
43 DontDestroyOnLoad(this.gameObject);
44
45 this.updateInterval = 1000 / PhotonNetwork.sendRate;
46 this.updateIntervalOnSerialize = 1000 / PhotonNetwork.sendRateOnSerialize;
47
48 PhotonHandler.StartFallbackSendAckThread();
49 }
File name: PhotonNetwork.cs
Copy
974 static PhotonNetwork()
975 {
976 #if UNITY_EDITOR
977 if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode)
978 {
979 //Debug.Log(string.Format("PhotonNetwork.ctor() Not playing {0} {1}", UnityEditor.EditorApplication.isPlaying, UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode));
980 return;
981 }
982
983 // This can happen when you recompile a script IN play made
984 // This helps to surpress some errors, but will not fix breaking
985 PhotonHandler[] photonHandlers = GameObject.FindObjectsOfType(typeof(PhotonHandler)) as PhotonHandler[];
986 if (photonHandlers != null && photonHandlers.Length > 0)
987 {
988 Debug.LogWarning("Unity recompiled. Connection gets closed and replaced. You can connect as 'new' client.");
989 foreach (PhotonHandler photonHandler in photonHandlers)
990 {
991 //Debug.Log("Handler: " + photonHandler + " photonHandler.gameObject: " + photonHandler.gameObject);
992 photonHandler.gameObject.hideFlags = 0;
993 GameObject.DestroyImmediate(photonHandler.gameObject);
994 Component.DestroyImmediate(photonHandler);
995 }
996 }
997 #endif
998
999 Application.runInBackground = true;
1000
1001 // Set up a MonoBehaviour to run Photon, and hide it
1002 GameObject photonGO = new GameObject();
1003 photonMono = (PhotonHandler)photonGO.AddComponent
1004 photonGO.name = "PhotonMono";
1005 photonGO.hideFlags = HideFlags.HideInHierarchy;
1006
1007
1008 // Set up the NetworkingPeer and use protocol of PhotonServerSettings
1009 networkingPeer = new NetworkingPeer(photonMono, string.Empty, PhotonNetwork.PhotonServerSettings.Protocol);
1010
1011
1012 // Local player
1013 CustomTypes.Register();
1014 }
File name: PhotonNetwork.cs
Copy
1072 public static void InternalCleanPhotonMonoFromSceneIfStuck()
1073 {
1074 PhotonHandler[] photonHandlers = GameObject.FindObjectsOfType(typeof(PhotonHandler)) as PhotonHandler[];
1075 if (photonHandlers != null && photonHandlers.Length > 0)
1076 {
1077 Debug.Log("Cleaning up hidden PhotonHandler instances in scene. Please save it. This is not an issue.");
1078 foreach (PhotonHandler photonHandler in photonHandlers)
1079 {
1080 // Debug.Log("Removing Handler: " + photonHandler + " photonHandler.gameObject: " + photonHandler.gameObject);
1081 photonHandler.gameObject.hideFlags = 0;
1082
1083 if (photonHandler.gameObject != null && photonHandler.gameObject.name == "PhotonMono")
1084 {
1085 GameObject.DestroyImmediate(photonHandler.gameObject);
1086 }
1087
1088 Component.DestroyImmediate(photonHandler);
1089 }
1090 }
1091 }
File name: AnimationBuilder.cs
Copy
57 public List58 {
59 var allAnimClips = AssetDatabase.LoadAllAssetRepresentationsAtPath(scmlAssetPath).OfType
60 Debug.Log(string.Format("Found {0} animation clips at {1}", allAnimClips.Count, scmlAssetPath));
61
62 var newAnimClips = new List
63
64 foreach (var animation in entity.Animations)
65 {
66 var animClip = MakeAnimationClip(root, animation, Path.GetDirectoryName(scmlAssetPath));
67 Debug.Log(string.Format("Added animClip({0}) to asset path ({1}) WrapMode:{2}", animClip.name, scmlAssetPath, animClip.wrapMode));
68 newAnimClips.Add(animClip);
69
70 var originalAnimClip = allAnimClips.Where(clip => clip.name == animClip.name).FirstOrDefault();
71 if (originalAnimClip != null)
72 {
73 Debug.Log("Replacing animation clip " + animClip.name);
74 EditorUtility.CopySerialized(animClip, originalAnimClip);
75 allAnimClips.Remove(originalAnimClip);
76 }
77 else
78 AssetDatabase.AddObjectToAsset(animClip, scmlAssetPath);
79 }
80
81 //Remove any animation clips that are no longer present in the SCML
82 foreach(var clip in allAnimClips)
83 {
84 //This may be a bad idea
85 UnityEngine.Object.DestroyImmediate(clip, true);
86 }
87
88 return newAnimClips;
89 }
File name: ScmlPostProcessor.cs
Copy
66 static void ImportScml(string assetPath)
67 {
68 string folderPath = Path.GetDirectoryName(assetPath);
69
70 //Load the SCML as XML
71 var doc = new XmlDocument();
72 doc.Load(assetPath);
73
74 //Parse the SCML file
75 var scml = new Spriter.ScmlObject(doc);
76
77 //TODO: Verify that all files/folders exist
78 var pb = new PrefabBuilder();
79 foreach (var entity in scml.Entities)
80 {
81 //TODO: Settings file to customize prefab location
82 var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab");
83
84 //Change to forward slash for asset database friendliness
85 prefabPath = prefabPath.Replace('\\', '/');
86
87 //Either instantiate the existing prefab or create a new one
88 GameObject go;
89 var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
90 if (prefabGo == null)
91 {
92 go = new GameObject();
93 prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab);
94 }
95 else
96 {
97 go = GameObject.Instantiate(prefabGo) as GameObject;
98
99 var oldAnimator = go.GetComponent
100 if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
101 }
102
103 //Build the prefab based on the supplied entity
104 pb.MakePrefab(entity, go, folderPath);
105
106 var animator = go.AddComponent
107
108
109
110 //Add animations to prefab object
111 var anim = new AnimationBuilder();
112 var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath);
113 AssetDatabase.SaveAssets();
114
115 var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller");
116 UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController));
117 UnityEditor.Animations.AnimatorController controller = oldController;
118
119 if (!oldController)
120 {
121 controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath);
122 foreach (var animationClip in allAnimClips)
123 {
124 if (animationClip)
125 {
126 //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
127 }
128 }
129 }
130 UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
131 go.SetActive(true);
132 //Update the prefab
133 PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab);
134
135 //Add a generic avatar - because why not?
136 //TODO: May need to eventually break this into a separate class
137 // ie: if we want to look for a root motion node by naming convention
138 //var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
139 //avatar.name = go.name;
140 //AssetDatabase.AddObjectToAsset(avatar, prefabPath);
141
142 GameObject.DestroyImmediate(go);
143
144 AssetDatabase.SaveAssets();
145 }
146 }
File name: ImportTiled2Unity.Mesh.cs
Copy
27 private void CreatePrefab(XElement xmlPrefab, string objPath)
28 {
29 var customImporters = GetCustomImporterInstances();
30
31 // Part 1: Create the prefab
32 string prefabName = xmlPrefab.Attribute("name").Value;
33 float prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
34 GameObject tempPrefab = new GameObject(prefabName);
35 HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);
36
37 // Part 2: Build out the prefab
38 AddGameObjectsTo(tempPrefab, xmlPrefab, objPath, customImporters);
39
40 // Part 3: Allow for customization from other editor scripts to be made on the prefab
41 // (These are generally for game-specific needs)
42 CustomizePrefab(tempPrefab, customImporters);
43
44 // Part 3.5: Apply the scale only after all children have been added
45 tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);
46
47 // Part 4: Save the prefab, keeping references intact.
48 string prefabPath = ImportUtils.GetPrefabPath(prefabName);
49 UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
50
51 if (finalPrefab == null)
52 {
53 // The prefab needs to be created
54 ImportUtils.ReadyToWrite(prefabPath);
55 finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
56 }
57
58 // Replace the prefab, keeping connections based on name.
59 PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);
60
61 // Destroy the instance from the current scene hiearchy.
62 UnityEngine.Object.DestroyImmediate(tempPrefab);
63 }
DestroyImmediate 103 lượt xem
Gõ tìm kiếm nhanh...