Created
January 18, 2017 18:35
-
-
Save jbruening/a3cf3f1020e422c662121774d7d63bce to your computer and use it in GitHub Desktop.
unity3d wizard to create BillboardAsset assets
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 System.Linq; | |
| using UnityEditor; | |
| using UnityEngine; | |
| class BillboardCreator : ScriptableWizard | |
| { | |
| private BillboardAsset _billboard; | |
| private SerializedObject _bs; | |
| private Mesh _mesh; | |
| private SerializedProperty materialProperty; | |
| private SerializedProperty heightProperty; | |
| private SerializedProperty bottomProperty; | |
| private SerializedProperty widthProperty; | |
| private int xSprites; | |
| private int ySprites; | |
| [MenuItem("Assets/Create/Billboard")] | |
| public static void CreateWizard() | |
| { | |
| var wizard = DisplayWizard<BillboardCreator>("Create scriptable object"); | |
| wizard.ResetAsset(); | |
| } | |
| private void ResetAsset() | |
| { | |
| if (_billboard != null) | |
| { | |
| Object.DestroyImmediate(_billboard, false); | |
| _mesh = null; | |
| } | |
| _billboard = new BillboardAsset(); | |
| _bs = new SerializedObject(_billboard); | |
| materialProperty = _bs.FindProperty("material"); | |
| heightProperty = _bs.FindProperty("height"); | |
| bottomProperty = _bs.FindProperty("bottom"); | |
| widthProperty = _bs.FindProperty("width"); | |
| } | |
| protected override bool DrawWizardGUI() | |
| { | |
| EditorGUILayout.PropertyField(materialProperty); | |
| EditorGUILayout.PropertyField(heightProperty); | |
| EditorGUILayout.PropertyField(bottomProperty); | |
| EditorGUILayout.PropertyField(widthProperty); | |
| _mesh = EditorGUILayout.ObjectField(_mesh, typeof(Mesh), false) as Mesh; | |
| xSprites = EditorGUILayout.IntField("x sprites", xSprites); | |
| ySprites = EditorGUILayout.IntField("y sprites", ySprites); | |
| return base.DrawWizardGUI(); | |
| } | |
| void OnWizardCreate() | |
| { | |
| _billboard.SetVertices(_mesh.vertices.Select(v => new Vector2(Mathf.Clamp01(v.x), Mathf.Clamp01(v.y))).ToArray()); | |
| _billboard.SetIndices(_mesh.GetIndices(0).Select(i => (ushort)i).ToArray()); | |
| var tex = new Vector4[xSprites * ySprites]; | |
| var wu = 1f / xSprites; | |
| var hv = 1f / ySprites; | |
| for(var x = 0; x < xSprites; x++) | |
| { | |
| for(var y = 0; y < ySprites; y++) | |
| { | |
| tex[x * y] = new Vector4(xSprites / (float)x, ySprites / (float)y, wu, hv); | |
| } | |
| } | |
| _billboard.SetImageTexCoords(tex); | |
| ProjectWindowUtil.CreateAsset(_billboard, "billboard.asset"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment