|
|
|
|
|
|
using System; |
|
|
|
using System.Collections; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEditor.Experimental.UIElements; |
|
|
|
using UnityEngine.Experimental.UIElements; |
|
|
|
|
|
|
ObjectField m_PreviewMeshPicker; |
|
|
|
|
|
|
|
MasterNode m_MasterNode; |
|
|
|
Mesh m_PreviousMesh; |
|
|
|
|
|
|
|
List<string> m_DoNotShowPrimitives = new List<string>( new string[] {PrimitiveType.Plane.ToString()}); |
|
|
|
|
|
|
|
static Type s_ContextualMenuManipulator = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).FirstOrDefault(t => t.FullName == "UnityEngine.Experimental.UIElements.ContextualMenuManipulator"); |
|
|
|
static Type s_ObjectSelector = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).FirstOrDefault(t => t.FullName == "UnityEditor.ObjectSelector"); |
|
|
|
this.clippingOptions = ClippingOptions.ClipAndCacheContents; |
|
|
|
m_Graph = graph; |
|
|
|
|
|
|
|
AddStyleSheetPath("Styles/MaterialGraph"); |
|
|
|
|
|
|
|
|
|
|
var topContainer = new VisualElement() { name = "top" }; |
|
|
|
{ |
|
|
|
var title = new Label(assetName + " master node preview") { name = "title" }; |
|
|
|
var title = new Label(assetName.Split('/').Last()) { name = "title" }; |
|
|
|
topContainer.Add(title); |
|
|
|
} |
|
|
|
Add(topContainer); |
|
|
|
|
|
|
m_PreviewTextureView = new PreviewTextureView { name = "preview", image = Texture2D.blackTexture }; |
|
|
|
m_PreviewTextureView.AddManipulator(new Draggable(OnMouseDragPreviwMesh, true)); |
|
|
|
m_PreviewTextureView.AddManipulator(new Draggable(OnMouseDragPreviewMesh, true)); |
|
|
|
m_PreviewTextureView.AddManipulator((IManipulator)Activator.CreateInstance(s_ContextualMenuManipulator, (Action<ContextualMenuPopulateEvent>)BuildContextualMenu)); |
|
|
|
|
|
|
|
middleContainer.Add(m_PreviewTextureView); |
|
|
|
|
|
|
|
m_PreviewScrollPosition = new Vector2(0f, 0f); |
|
|
|
|
|
|
Add(middleContainer); |
|
|
|
} |
|
|
|
var bottomContainer = new VisualElement() { name = "bottom" }; |
|
|
|
void BuildContextualMenu(ContextualMenuPopulateEvent evt) |
|
|
|
{ |
|
|
|
foreach (var primitiveTypeName in Enum.GetNames(typeof(PrimitiveType))) |
|
|
|
m_PreviewMeshPicker = new ObjectField { name = "picker", objectType = typeof(Mesh) }; |
|
|
|
m_PreviewMeshPicker.OnValueChanged(OnPreviewMeshChanged); |
|
|
|
|
|
|
|
bottomContainer.Add(m_PreviewMeshPicker); |
|
|
|
if(m_DoNotShowPrimitives.Contains(primitiveTypeName)) |
|
|
|
continue; |
|
|
|
evt.menu.AppendAction(primitiveTypeName, e => ChangePrimitiveMesh( primitiveTypeName ), ContextualMenu.MenuAction.AlwaysEnabled); |
|
|
|
Add(bottomContainer); |
|
|
|
|
|
|
|
evt.menu.AppendAction("Custom Mesh", e => ChangeMeshCustom(), ContextualMenu.MenuAction.AlwaysEnabled); |
|
|
|
} |
|
|
|
|
|
|
|
MasterNode masterNode |
|
|
|
|
|
|
m_PreviewTextureView.Dirty(ChangeType.Repaint); |
|
|
|
} |
|
|
|
|
|
|
|
void OnPreviewMeshChanged(ChangeEvent<Object> changeEvent) |
|
|
|
void ChangePrimitiveMesh(string primitiveName) |
|
|
|
Mesh changedMesh = changeEvent.newValue as Mesh; |
|
|
|
Mesh changedPrimitiveMesh = Resources.GetBuiltinResource(typeof(Mesh), string.Format("{0}.fbx", primitiveName)) as Mesh; |
|
|
|
|
|
|
|
ChangeMesh(changedPrimitiveMesh); |
|
|
|
} |
|
|
|
|
|
|
|
void ChangeMesh(Mesh mesh) |
|
|
|
{ |
|
|
|
Mesh changedMesh = mesh; |
|
|
|
|
|
|
|
masterNode.Dirty(ModificationScope.Node); |
|
|
|
|
|
|
|
|
|
|
m_Graph.previewData.serializedMesh.mesh = changedMesh; |
|
|
|
} |
|
|
|
|
|
|
|
private static EditorWindow Get() |
|
|
|
{ |
|
|
|
PropertyInfo P = s_ObjectSelector.GetProperty("get", BindingFlags.Public | BindingFlags.Static); |
|
|
|
return P.GetValue(null,null) as EditorWindow; |
|
|
|
} |
|
|
|
|
|
|
|
void OnMeshChanged(Object obj) |
|
|
|
{ |
|
|
|
var mesh = obj as Mesh; |
|
|
|
if (mesh == null) |
|
|
|
mesh = m_PreviousMesh; |
|
|
|
ChangeMesh(mesh); |
|
|
|
} |
|
|
|
|
|
|
|
void ChangeMeshCustom() |
|
|
|
{ |
|
|
|
MethodInfo ShowMethod = s_ObjectSelector.GetMethod("Show", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, Type.DefaultBinder, new[] {typeof(Object), typeof(Type), typeof(SerializedProperty), typeof(bool), typeof(List<int>), typeof(Action<Object>), typeof(Action<Object>)}, new ParameterModifier[7]); |
|
|
|
m_PreviousMesh = m_Graph.previewData.serializedMesh.mesh; |
|
|
|
ShowMethod.Invoke(Get(), new object[] { null, typeof(Mesh), null, false, null, (Action<Object>)OnMeshChanged, (Action<Object>)OnMeshChanged }); |
|
|
|
} |
|
|
|
|
|
|
|
public void RefreshRenderTextureSize() |
|
|
|
{ |
|
|
|
RenderTextureDescriptor descriptor = m_PreviewRenderHandle.renderTexture.descriptor; |
|
|
|
|
|
|
RefreshRenderTextureSize(); |
|
|
|
} |
|
|
|
|
|
|
|
void OnMouseDragPreviwMesh(Vector2 deltaMouse) |
|
|
|
void OnMouseDragPreviewMesh(Vector2 deltaMouse) |
|
|
|
{ |
|
|
|
Vector2 previewSize = m_PreviewTextureView.contentRect.size; |
|
|
|
|
|
|
|