MeshPreview.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace SoftKitty
  4. {
  5. public class MeshPreview : EditorWindow
  6. {
  7. Editor gameObjectEditor;
  8. static GameObject gameObject;
  9. public static MeshRenderer renderer;
  10. public static MeshPreview instance;
  11. public static void ShowPreview(string _meshPath, string _matPath, string _path)
  12. {
  13. gameObject = (GameObject)AssetDatabase.LoadAssetAtPath(_path, typeof(GameObject));
  14. if (_meshPath != "")
  15. {
  16. MeshFilter _mf = gameObject.GetComponentInChildren<MeshFilter>();
  17. _mf.mesh = Instantiate(Resources.Load<Mesh>(_meshPath));
  18. }
  19. if (_matPath!="") {
  20. renderer = gameObject.GetComponentInChildren<MeshRenderer>();
  21. Material _mat = Instantiate(Resources.Load<Material>(_matPath)); ;
  22. renderer.material = _mat;
  23. renderer.sharedMaterial = _mat;
  24. }
  25. instance=GetWindowWithRect<MeshPreview>(new Rect(0, 0, 256, 256));
  26. }
  27. private void OnDestroy()
  28. {
  29. renderer = null;
  30. }
  31. void OnGUI()
  32. {
  33. GUIStyle bgColor = new GUIStyle();
  34. if (gameObjectEditor == null)
  35. {
  36. gameObjectEditor = Editor.CreateEditor(gameObject);
  37. }
  38. gameObjectEditor.OnInteractivePreviewGUI(GUILayoutUtility.GetRect(256, 256), bgColor);
  39. }
  40. }
  41. }