CreateAsset
How do I use Create Asset
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: 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
78 private void CreateMaterialsFromInternalTextures(XDocument xml)
79 {
80 var texData = xml.Root.Elements("InternalTexture");
81 foreach (var tex in texData)
82 {
83 string texAssetPath = tex.Attribute("assetPath").Value;
84 string materialPath = ImportUtils.GetMaterialPath(texAssetPath);
85
86 Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
87 if (material == null)
88 {
89 // Create our material
90 material = new Material(Shader.Find("Tiled/TextureTintSnap"));
91
92 // Assign to it the texture that is already internal to our Unity project
93 Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texAssetPath, typeof(Texture2D)) as Texture2D;
94 material.SetTexture("_MainTex", texture2d);
95
96 // Write the material to our asset database
97 ImportUtils.ReadyToWrite(materialPath);
98 AssetDatabase.CreateAsset(material, materialPath);
99 }
100 }
101 }
CreateAsset 116 lượt xem
Gõ tìm kiếm nhanh...