Question by
wangquan84 · May 28, 2017 at 05:14 AM ·
editor-scriptingeditor window
Render GameObject in editorWidnow & How to hide these Objects in Scene and Game window?
Use PreviewRenderUtility draw some object in EditorWindow. But I want to these object hide in Scene or Game Window.
Btw I need to selected Object in EditorWindow Use Physics.Raycast API. So I can't Delete GameObject.
What's the best way to render a Mesh in an Editorwindow? How do I solve the problems I'm having? I'm appreciated that someone can help me out! SOS!!!
Here some code below:
private PreviewRenderUtility renderer;
private void InitRenderUtility(){
renderer = new PreviewRenderUtility();
renderer.m_Camera.nearClipPlane = 0.3f;
renderer.m_Camera.farClipPlane = 1000f;
renderer.m_Camera.transform.position = new Vector3(0, 0, -10);
renderer.m_Camera.clearFlags = CameraClearFlags.Depth;
renderer.m_Camera.depth = -1;
renderer.m_Camera.orthographic = true;
}
public void OnGUI(){
renderer.BeginPreview(frameViewRect, GUIStyle.none);
GameObject obj = CreateSomeObject();
var objMesh = obj.GetComponent<MeshFilter>().sharedMesh;
var objMat = obj.GetComponent<MeshRenderer>().sharedMaterial;
Matrix4x4 m = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
renderer.DrawMesh(objMesh, m, objMat, 0);
renderer.m_Camera.Render();
var tex = renderer.EndPreview();
GUI.DrawTexture(frameViewRect, tex);
}
private GameObject CreateSomeObject(){
GameObject obj = EditorUtility.CreateGameObjectWithHideFlags(name, HideFlags.HideAndDontSave,
new System.Type[] {typeof(MeshRenderer),typeof(MeshFilter)});
MeshFilter filter = obj.GetComponent<MeshFilter> ();
MeshRenderer render = obj.GetComponent<MeshRenderer> ();
MeshCollider collider = obj.AddComponent<MeshCollider>();
render.material = new Material(someMaterial);
Mesh mesh = new Mesh ();
mesh.hideFlags = HideFlags.HideAndDontSave;
mesh.vertices = newVertices;
mesh.uv = newUV;
mesh.triangles = meshTriangles;
filter.sharedMesh = mesh;
collider.sharedMesh = mesh;
return obj;
}
Comment