ImportAsset
How do I use Import Asset
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonConverter.cs
Copy
160 static void ConvertScripts(List161 {
162 bool ignoreWarningIsLogged = false;
163
164 foreach (string script in scriptPathList)
165 {
166 if (script.Contains("PhotonNetwork")) //Don't convert this file (and others)
167 {
168 if (!ignoreWarningIsLogged)
169 {
170 ignoreWarningIsLogged = true;
171 Debug.LogWarning("Conversion to PUN ignores all files with \"PhotonNetwork\" in their file-path.\nCheck: " + script);
172 }
173 continue;
174 }
175 if (script.Contains("Image Effects"))
176 {
177 continue;
178 }
179
180 ConvertToPhotonAPI(script);
181 }
182
183 foreach (string script in scriptPathList)
184 {
185 AssetDatabase.ImportAsset(script, ImportAssetOptions.ForceUpdate);
186 }
187 }
File name: PhotonConverter.cs
Copy
341 static void EnsureFolder(string path)
342 {
343 if (!Directory.Exists(path))
344 {
345 Directory.CreateDirectory(path);
346 AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
347 AssetDatabase.Refresh();
348 }
349 }
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: ImportTiled2Unity.Xml.cs
Copy
42 private void ImportTexturesFromXml(XDocument xml)
43 {
44 var texData = xml.Root.Elements("ImportTexture");
45 foreach (var tex in texData)
46 {
47 string name = tex.Attribute("filename").Value;
48 string data = tex.Value;
49
50 // The data is gzip compressed base64 string. We need the raw bytes.
51 //byte[] bytes = ImportUtils.GzipBase64ToBytes(data);
52 byte[] bytes = ImportUtils.Base64ToBytes(data);
53
54 // Save and import the texture asset
55 {
56 string pathToSave = ImportUtils.GetTexturePath(name);
57 ImportUtils.ReadyToWrite(pathToSave);
58 File.WriteAllBytes(pathToSave, bytes);
59 AssetDatabase.ImportAsset(pathToSave, ImportAssetOptions.ForceSynchronousImport);
60 }
61
62 // Create a material if needed in prepartion for the texture being successfully imported
63 {
64 string materialPath = ImportUtils.GetMaterialPath(name);
65 Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
66 if (material == null)
67 {
68 // We need to create the material afterall
69 // Use our custom shader
70 material = new Material(Shader.Find("Tiled/TextureTintSnap"));
71 ImportUtils.ReadyToWrite(materialPath);
72 AssetDatabase.CreateAsset(material, materialPath);
73 }
74 }
75 }
76 }
File name: ImportTiled2Unity.Xml.cs
Copy
103 private void ImportMeshesFromXml(XDocument xml)
104 {
105 var meshData = xml.Root.Elements("ImportMesh");
106 foreach (var mesh in meshData)
107 {
108 // We're going to create/write a file that contains our mesh data as a Wavefront Obj file
109 // The actual mesh will be imported from this Obj file
110
111 string name = mesh.Attribute("filename").Value;
112 string data = mesh.Value;
113
114 // The data is in base64 format. We need it as a raw string.
115 string raw = ImportUtils.Base64ToString(data);
116
117 // Save and import the asset
118 string pathToMesh = "Assets/Tiled2Unity/Meshes/" + name;
119 ImportUtils.ReadyToWrite(pathToMesh);
120 File.WriteAllText(pathToMesh, raw, Encoding.UTF8);
121 AssetDatabase.ImportAsset(pathToMesh, ImportAssetOptions.ForceSynchronousImport);
122 }
123 }
ImportAsset 122 lượt xem
Gõ tìm kiếm nhanh...