private static LightbeamSettings GetLigthbeamSettings(Lightbeam lightBeam) { LightbeamSettings settings = null; MeshFilter meshFilter = lightBeam.GetComponent <MeshFilter>(); if (meshFilter != null && meshFilter.sharedMesh != null) { foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(meshFilter.sharedMesh))) { if (asset is LightbeamSettings) { settings = asset as LightbeamSettings; } } } return(settings); }
private static void SaveNewAsset(Lightbeam lightBeam, bool duplicateMaterial) { MeshRenderer meshRenderer = (MeshRenderer)lightBeam.GetComponent(typeof(MeshRenderer)); if (meshRenderer == null) meshRenderer = lightBeam.gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer; MeshFilter meshFilter = (MeshFilter)lightBeam.GetComponent(typeof(MeshFilter)); if (meshFilter == null) meshFilter = lightBeam.gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; Mesh mesh = meshFilter.sharedMesh; if (mesh == null) mesh = new Mesh(); LightbeamSettings settings = GetLigthbeamSettings(lightBeam); if (settings != null) lightBeam.Settings = ScriptableObject.Instantiate(settings) as LightbeamSettings; else lightBeam.Settings = ScriptableObject.CreateInstance<LightbeamSettings>(); lightBeam.Settings.hideFlags = HideFlags.HideInHierarchy; Material material = null; Material sourceMaterial = null; if (meshRenderer.sharedMaterial != null) { if (duplicateMaterial) material = new Material(meshRenderer.sharedMaterial); else material = meshRenderer.sharedMaterial; sourceMaterial = meshRenderer.sharedMaterial; } // The default material can be set on the script file from the inspector, this is the preferred way of doing it since you can choose what material to use as default. if (material == null && lightBeam.DefaultMaterial != null) material = new Material(lightBeam.DefaultMaterial); // If the default material wasn't set, try to find the material from the script path. if (material == null) { // Get the light beam folder by getting the path to the script and using that path to find the material. MonoScript script = MonoScript.FromMonoBehaviour(lightBeam); string scriptPath = AssetDatabase.GetAssetPath(script); scriptPath = Path.GetDirectoryName(scriptPath); string[] directories = scriptPath.Split('/'); scriptPath = string.Join("/", directories, 0, directories.Length - 1); string materialPath = scriptPath + "/Source/Lightbeam.mat"; Material mat = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; if (mat != null) material = new Material(mat); } // If the material still is null we'll create a new material and assign the shader to it. if (material == null) material = new Material(Shader.Find("Lightbeam/Lightbeam")); meshRenderer.sharedMaterial = material; // Copy mesh Mesh newMesh = new Mesh(); newMesh.Clear(); newMesh.vertices = mesh.vertices; newMesh.triangles = mesh.triangles; newMesh.normals = mesh.vertices; newMesh.uv = mesh.uv; newMesh.tangents = mesh.tangents; newMesh.colors = mesh.colors; // Save the mesh asset // Find a free name to use string savePath = ""; string assetPath = AssetDatabase.GetAssetPath(meshFilter.sharedMesh); if (assetPath == "") assetPath = "Assets/lightbeam"; else assetPath = Path.GetDirectoryName(assetPath) + "/" + Path.GetFileNameWithoutExtension(assetPath); int i = 0; while (true) { savePath = assetPath + "_" + i + ".asset"; if (File.Exists(savePath) == false) break; i++; } AssetDatabase.CreateAsset(newMesh, savePath); AssetDatabase.AddObjectToAsset(lightBeam.Settings, newMesh); AssetDatabase.ImportAsset(savePath); // Save the material string materialAssetPath = AssetDatabase.GetAssetPath(sourceMaterial); if (materialAssetPath == "" || duplicateMaterial) { if (materialAssetPath == "") materialAssetPath = "Assets/lightbeam"; else materialAssetPath = Path.GetDirectoryName(materialAssetPath) + "/" + Path.GetFileNameWithoutExtension(materialAssetPath); savePath = ""; i = 0; while (true) { savePath = materialAssetPath + "_" + i + ".mat"; if (File.Exists(savePath) == false) break; i++; } AssetDatabase.CreateAsset(material, savePath); } meshFilter.sharedMesh = newMesh; meshRenderer.sharedMaterial = material; }
private static LightbeamSettings GetLigthbeamSettings(Lightbeam lightBeam) { LightbeamSettings settings = null; MeshFilter meshFilter = lightBeam.GetComponent<MeshFilter>(); if (meshFilter != null && meshFilter.sharedMesh != null) { foreach (var asset in AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(meshFilter.sharedMesh))) { if (asset is LightbeamSettings) { settings = asset as LightbeamSettings; } } } return settings; }
private static void SaveNewAsset(Lightbeam lightBeam, bool duplicateMaterial) { MeshRenderer meshRenderer = (MeshRenderer)lightBeam.GetComponent(typeof(MeshRenderer)); if (meshRenderer == null) { meshRenderer = lightBeam.gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer; } MeshFilter meshFilter = (MeshFilter)lightBeam.GetComponent(typeof(MeshFilter)); if (meshFilter == null) { meshFilter = lightBeam.gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter; } Mesh mesh = meshFilter.sharedMesh; if (mesh == null) { mesh = new Mesh(); } LightbeamSettings settings = GetLigthbeamSettings(lightBeam); if (settings != null) { lightBeam.Settings = ScriptableObject.Instantiate(settings) as LightbeamSettings; } else { lightBeam.Settings = ScriptableObject.CreateInstance <LightbeamSettings>(); } lightBeam.Settings.hideFlags = HideFlags.HideInHierarchy; Material material = null; Material sourceMaterial = null; if (meshRenderer.sharedMaterial != null) { if (duplicateMaterial) { material = new Material(meshRenderer.sharedMaterial); } else { material = meshRenderer.sharedMaterial; } sourceMaterial = meshRenderer.sharedMaterial; } // The default material can be set on the script file from the inspector, this is the preferred way of doing it since you can choose what material to use as default. if (material == null && lightBeam.DefaultMaterial != null) { material = new Material(lightBeam.DefaultMaterial); } // If the default material wasn't set, try to find the material from the script path. if (material == null) { // Get the light beam folder by getting the path to the script and using that path to find the material. MonoScript script = MonoScript.FromMonoBehaviour(lightBeam); string scriptPath = AssetDatabase.GetAssetPath(script); scriptPath = Path.GetDirectoryName(scriptPath); string[] directories = scriptPath.Split('/'); scriptPath = string.Join("/", directories, 0, directories.Length - 1); string materialPath = scriptPath + "/Source/Lightbeam.mat"; Material mat = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material; if (mat != null) { material = new Material(mat); } } // If the material still is null we'll create a new material and assign the shader to it. if (material == null) { material = new Material(Shader.Find("Lightbeam/Lightbeam")); } meshRenderer.sharedMaterial = material; // Copy mesh Mesh newMesh = new Mesh(); newMesh.Clear(); newMesh.vertices = mesh.vertices; newMesh.triangles = mesh.triangles; newMesh.normals = mesh.vertices; newMesh.uv = mesh.uv; newMesh.tangents = mesh.tangents; newMesh.colors = mesh.colors; // Save the mesh asset // Find a free name to use string savePath = ""; string assetPath = AssetDatabase.GetAssetPath(meshFilter.sharedMesh); if (assetPath == "") { assetPath = "Assets/lightbeam"; } else { assetPath = Path.GetDirectoryName(assetPath) + "/" + Path.GetFileNameWithoutExtension(assetPath); } int i = 0; while (true) { savePath = assetPath + "_" + i + ".asset"; if (File.Exists(savePath) == false) { break; } i++; } AssetDatabase.CreateAsset(newMesh, savePath); AssetDatabase.AddObjectToAsset(lightBeam.Settings, newMesh); AssetDatabase.ImportAsset(savePath); // Save the material string materialAssetPath = AssetDatabase.GetAssetPath(sourceMaterial); if (materialAssetPath == "" || duplicateMaterial) { if (materialAssetPath == "") { materialAssetPath = "Assets/lightbeam"; } else { materialAssetPath = Path.GetDirectoryName(materialAssetPath) + "/" + Path.GetFileNameWithoutExtension(materialAssetPath); } savePath = ""; i = 0; while (true) { savePath = materialAssetPath + "_" + i + ".mat"; if (File.Exists(savePath) == false) { break; } i++; } AssetDatabase.CreateAsset(material, savePath); } meshFilter.sharedMesh = newMesh; meshRenderer.sharedMaterial = material; }