Created
December 18, 2011 11:44
-
-
Save N-Carter/1493109 to your computer and use it in GitHub Desktop.
EditorActions.UpdateLayerEnum()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.IO; | |
public class EditorActions | |
{ | |
[MenuItem ("Toolkit/Tags and Layers/Update Layer.cs")] | |
static void UpdateLayerConstants() | |
{ | |
// Writes a file named Layer.cs containing a Layer class with constants for each layer as both an | |
// index number and a mask. | |
string filename = GetFullPathToFirstAssetWithFilename("Layer.cs"); | |
if(!string.IsNullOrEmpty(filename)) | |
{ | |
if(!EditorUtility.DisplayDialog("Overwrite existing Layer.cs file?", string.Format("Full path: {0}", filename), "Overwrite", "Cancel")) | |
return; | |
} | |
else | |
{ | |
filename = Application.dataPath + "/Layer.cs"; | |
if(!EditorUtility.DisplayDialog("Create a new Layer.cs file?", string.Format("Full path: {0}", filename), "Create", "Cancel")) | |
return; | |
} | |
using(var fileStream = File.CreateText(filename)) | |
{ | |
fileStream.WriteLine("// File generated by EditorActions.UpdateLayerConstants()"); | |
fileStream.WriteLine(); | |
fileStream.WriteLine("public static class Layer"); | |
fileStream.WriteLine("{"); | |
for(int i = 0; i < 32; ++i) | |
{ | |
var name = LayerMask.LayerToName(i); | |
if(!string.IsNullOrEmpty(name)) | |
{ | |
name = name.Replace(" ", string.Empty); | |
fileStream.WriteLine("\tpublic const int {0} = {1};", name + "Index", i); | |
fileStream.WriteLine("\tpublic const int {0} = 0x{1:X8};", name + "Mask", 1 << i); | |
} | |
} | |
fileStream.WriteLine("}"); | |
} | |
AssetDatabase.Refresh(); | |
} | |
static string GetFullPathToFirstAssetWithFilename(string filename) | |
{ | |
var files = Directory.GetFiles(Application.dataPath, filename, SearchOption.AllDirectories); | |
return (files.Length > 0 ? files[0] : ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment