// ------------------------------------------------------------------ /// \param _spriteFont the sprite font /// build the sprite font // ------------------------------------------------------------------ public static void Build(this exSpriteFont _spriteFont) { #if UNITY_3_4 bool isPrefab = (EditorUtility.GetPrefabType(_spriteFont) == PrefabType.Prefab); #else bool isPrefab = (PrefabUtility.GetPrefabType(_spriteFont) == PrefabType.Prefab); #endif // when build, alway set dirty EditorUtility.SetDirty(_spriteFont); if (_spriteFont.fontInfo == null) { _spriteFont.clippingPlane = null; GameObject.DestroyImmediate(_spriteFont.meshFilter.sharedMesh, true); _spriteFont.meshFilter.sharedMesh = null; _spriteFont.renderer.sharedMaterial = null; return; } // prefab do not need rebuild mesh if (isPrefab == false) { exClipping clipping = _spriteFont.clippingPlane; if (clipping != null) { clipping.RemovePlane(_spriteFont); } // Mesh newMesh = new Mesh(); newMesh.hideFlags = HideFlags.DontSave; newMesh.Clear(); // update material _spriteFont.renderer.sharedMaterial = _spriteFont.fontInfo.pageInfos[0].material; // update mesh _spriteFont.ForceUpdateMesh(newMesh); // GameObject.DestroyImmediate(_spriteFont.meshFilter.sharedMesh, true); // delete old mesh (to avoid leaking) _spriteFont.meshFilter.sharedMesh = newMesh; // if (clipping != null) { clipping.AddPlaneInEditor(_spriteFont); } } // update collider if (_spriteFont.collisionHelper) { _spriteFont.collisionHelper.UpdateCollider(); } }
// ------------------------------------------------------------------ /// \param _sprite the sprite /// \param _texture the raw texture used in the sprite /// build the sprite by texture // ------------------------------------------------------------------ public static void Build(this exSprite _sprite, Texture2D _texture = null) { #if UNITY_3_4 bool isPrefab = (EditorUtility.GetPrefabType(_sprite) == PrefabType.Prefab); #else bool isPrefab = (PrefabUtility.GetPrefabType(_sprite) == PrefabType.Prefab); #endif EditorUtility.SetDirty(_sprite); // if (_sprite.atlas == null && _texture == null) { _sprite.clippingPlane = null; GameObject.DestroyImmediate(_sprite.meshFilter.sharedMesh, true); _sprite.meshFilter.sharedMesh = null; _sprite.renderer.sharedMaterial = null; return; } exClipping clipping = _sprite.clippingPlane; if (clipping != null) { clipping.RemovePlane(_sprite); } // set a texture to it if (_sprite.atlas != null) { _sprite.renderer.sharedMaterial = _sprite.atlas.material; } else if (_texture != null) { _sprite.renderer.sharedMaterial = exEditorHelper.GetDefaultMaterial(_texture, _texture.name); } EditorUtility.UnloadUnusedAssets(); // if (_sprite.useAtlas == false && _sprite.customSize == false && _sprite.trimTexture == false) { _sprite.width = _texture.width; _sprite.height = _texture.height; } // prefab do not need rebuild mesh if (isPrefab == false) { // NOTE: it is possible user duplicate an GameObject, // if we directly change the mesh, the original one will changed either. Mesh newMesh = new Mesh(); newMesh.hideFlags = HideFlags.DontSave; newMesh.Clear(); // build vertices, normals, uvs and colors. _sprite.ForceUpdateMesh(newMesh); // set the new mesh in MeshFilter GameObject.DestroyImmediate(_sprite.meshFilter.sharedMesh, true); // delete old mesh (to avoid leaking) _sprite.meshFilter.sharedMesh = newMesh; } // update collider if (_sprite.collisionHelper) { _sprite.collisionHelper.UpdateCollider(); } // if (clipping != null) { clipping.AddPlaneInEditor(_sprite); } }
// ------------------------------------------------------------------ // Desc: // ------------------------------------------------------------------ public override void OnInspectorGUI() { base.OnInspectorGUI(); GUILayout.Space(20); EditorGUIUtility.LookLikeInspector(); // ======================================================== // width // ======================================================== GUI.enabled = !inAnimMode; curEdit.width = EditorGUILayout.FloatField("Width", curEdit.width); GUI.enabled = true; // ======================================================== // height // ======================================================== GUI.enabled = !inAnimMode; curEdit.height = EditorGUILayout.FloatField("Height", curEdit.height); GUI.enabled = true; // ======================================================== // // ======================================================== curEdit.isDyanmic = EditorGUILayout.Toggle("Is Dyanmic", curEdit.isDyanmic); // ======================================================== // clip material list // ======================================================== showMaterialList = EditorGUILayout.Foldout(showMaterialList, "Clip Materials"); if (showMaterialList) { ++EditorGUI.indentLevel; GUI.enabled = false; for (int i = 0; i < curEdit.clipMaterialList.Count; ++i) { Material clipMat = curEdit.clipMaterialList[i]; EditorGUILayout.ObjectField("[" + i + "]" , clipMat , typeof(Material) , true ); } --EditorGUI.indentLevel; GUI.enabled = true; } // ======================================================== // clip list // ======================================================== Rect lastRect = new Rect(0, 0, 1, 1); Rect dropRect = new Rect(0, 0, 1, 1); showClipList = EditorGUILayout.Foldout(showClipList, "Clip Objects"); if (showClipList) { ++EditorGUI.indentLevel; int idxRemoved = -1; for (int i = 0; i < curEdit.planeInfoList.Count; ++i) { GUILayout.BeginHorizontal(); exPlane curPlane = curEdit.planeInfoList[i].plane; exPlane newPlane = (exPlane)EditorGUILayout.ObjectField("[" + i + "]" , curPlane , typeof(exPlane) , true ); if (newPlane != curPlane && curEdit.HasPlaneInfo(newPlane) == false) { curEdit.RemovePlaneInEditor(curPlane); curEdit.InsertPlaneInEditor(i, newPlane); } if (GUILayout.Button("-", GUILayout.Width(15), GUILayout.Height(15))) { idxRemoved = i; } GUILayout.EndHorizontal(); } // if we have item to remove if (idxRemoved != -1) { curEdit.RemovePlaneInEditor(curEdit.planeInfoList[idxRemoved].plane); } --EditorGUI.indentLevel; EditorGUILayout.Space(); lastRect = GUILayoutUtility.GetLastRect(); dropRect.x = lastRect.x + 30; dropRect.y = lastRect.yMax; dropRect.width = lastRect.xMax - 30 - 4; dropRect.height = 20; exEditorHelper.DrawRect(dropRect, new Color(0.2f, 0.2f, 0.2f, 1.0f), new Color(0.5f, 0.5f, 0.5f, 1.0f)); GUILayout.Space(20); // ======================================================== // drag and drop // ======================================================== if (dropRect.Contains(Event.current.mousePosition)) { if (Event.current.type == EventType.DragUpdated) { // Show a copy icon on the drag foreach (Object o in DragAndDrop.objectReferences) { if (o is GameObject) { if ((o as GameObject).GetComponent <exPlane>() != null) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; break; } } } } else if (Event.current.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); foreach (Object o in DragAndDrop.objectReferences) { if (o is GameObject) { exPlane plane = (o as GameObject).GetComponent <exPlane>(); if (plane) { curEdit.AddPlaneInEditor(plane); } } } GUI.changed = true; } } } EditorGUILayout.Space(); // ======================================================== // if changes // ======================================================== if (GUI.changed) { EditorUtility.SetDirty(curEdit); } }