void OnGUI() { EditorGUILayout.Space(); Material newMaterial = EditorGUILayout.ObjectField("Material", _prototype._material, typeof(Material), false) as Material; _parent.SetPrototypeMaterial(_prototype, newMaterial); EditorGUI.BeginChangeCheck(); _prototype._minWidth = EditorGUILayout.FloatField("Min Width", _prototype._minWidth); _prototype._maxWidth = EditorGUILayout.FloatField("Max Width", _prototype._maxWidth); _prototype._minHeight = EditorGUILayout.FloatField("Min Height", _prototype._minHeight); _prototype._maxHeight = EditorGUILayout.FloatField("Max Height", _prototype._maxHeight); if (EditorGUI.EndChangeCheck()) { _parent.UpdateDetailsSize(_prototype); UtilityFuncs.MarkCurrentSceneIsDirty(); } }
private bool AddDetailObjs(GrassPrototype prototype, int x, int z, int amount) { for (int i = 0; i < amount; i++) { Vector3 pos = new Vector3(x + Random.Range(0.0f, 1.0f), 0, z + Random.Range(0.0f, 1.0f)); Ray ray = new Ray(new Vector3(pos.x, 100000, pos.z), Vector3.down); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, _editor._layerMask)) { pos.y = hit.point.y; GameObject go = new GameObject(prototype._material.name); //Undo.RegisterCreatedObjectUndo(go, go.name); //go.layer = LayerMask.NameToLayer("Vegetation"); //go.hideFlags = HideFlags.HideInHierarchy; go.tag = "EditorOnly"; GameObjectUtility.SetStaticEditorFlags(go, StaticEditorFlags.LightmapStatic | StaticEditorFlags.BatchingStatic); go.hideFlags = HideFlags.NotEditable; go.transform.position = pos; go.transform.localEulerAngles = new Vector3(0, Random.Range(0, 360), 0); float width = Random.Range(prototype._minWidth, prototype._maxWidth); float height = Random.Range(prototype._minHeight, prototype._maxHeight); go.transform.localScale = new Vector3(width, height, 1); go.transform.SetParent(GrassRootGo.transform); MeshFilter meshFilter = go.AddComponent <MeshFilter>(); Mesh mesh = _editor._grassMesh; meshFilter.mesh = mesh; MeshRenderer meshRenderer = go.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = prototype._material; SerializedObject so = new SerializedObject(meshRenderer); SerializedProperty goScaleProperty = so.FindProperty("m_ScaleInLightmap"); goScaleProperty.floatValue = _editor._scaleInLightmap; so.ApplyModifiedProperties(); GrassObject info = go.AddComponent <GrassObject>(); info._material = prototype._material; //info._x = x; //info._z = z; //fix y if (_editor._isKeepInDeep) { float minY = pos.y; for (int v = 0; v < mesh.vertexCount; v++) { if (mesh.vertices[v].y == 0) { Vector3 posWorld = go.transform.TransformPoint(mesh.vertices[v]); if (Physics.Raycast(posWorld, Vector3.down, out hit, Mathf.Infinity, _editor._layerMask)) { if (hit.point.y < minY) { minY = hit.point.y; } } } } if (minY < pos.y) { pos.y = minY; go.transform.position = pos; } } UtilityFuncs.MarkCurrentSceneIsDirty(); } } return(true); }
private void DestoryObj(GameObject obj) { DestroyImmediate(obj); UtilityFuncs.MarkCurrentSceneIsDirty(); //Undo.DestroyObjectImmediate(obj); }
public void ExportMeshToFile(GameObject go) { string path = EditorUtility.SaveFilePanelInProject("Save Mesh", go.name, "asset", "Please enter a file name to save the mesh(es) to"); if (!string.IsNullOrEmpty(path)) { Dictionary <int, MeshFilter> index2MeshFilter = new Dictionary <int, MeshFilter>(); Dictionary <int, SkinnedMeshRenderer> index2SkinnedRenderer = new Dictionary <int, SkinnedMeshRenderer>(); List <Mesh> exportMeshes = new List <Mesh>(); { MeshFilter[] meshFilters; if (_includeAllChildren) { meshFilters = go.GetComponentsInChildren <MeshFilter>(_includeInactive); } else { meshFilters = new MeshFilter[1]; meshFilters[0] = go.GetComponent <MeshFilter>(); } foreach (MeshFilter meshFilter in meshFilters) { if (meshFilter != null && meshFilter.sharedMesh != null) { Mesh mesh = CopyMesh(meshFilter.sharedMesh.name, meshFilter.sharedMesh, false); if (mesh != null) { if (_autoAssignMesh) { index2MeshFilter.Add(exportMeshes.Count, meshFilter); } exportMeshes.Add(mesh); } } } } { SkinnedMeshRenderer[] skinnedRenderers; if (_includeAllChildren) { skinnedRenderers = go.GetComponentsInChildren <SkinnedMeshRenderer>(_includeInactive); } else { skinnedRenderers = new SkinnedMeshRenderer[1]; skinnedRenderers[0] = go.GetComponent <SkinnedMeshRenderer>(); } foreach (SkinnedMeshRenderer skinnedRenderer in skinnedRenderers) { if (skinnedRenderer != null && skinnedRenderer.sharedMesh != null) { Mesh mesh = CopyMesh(skinnedRenderer.sharedMesh.name, skinnedRenderer.sharedMesh, true); if (mesh != null) { if (_autoAssignMesh) { index2SkinnedRenderer.Add(exportMeshes.Count, skinnedRenderer); } exportMeshes.Add(mesh); } } } } if (exportMeshes.Count > 0) { SaveToFile(path, exportMeshes); AssetDatabase.ImportAsset(path); MeshExportData meshExportData = AssetDatabase.LoadAssetAtPath(path, typeof(MeshExportData)) as MeshExportData; if (_autoAssignMesh) { for (int i = 0; i < meshExportData._meshes.Length; i++) { MeshFilter meshFilterOrgin; if (index2MeshFilter.TryGetValue(i, out meshFilterOrgin)) { meshFilterOrgin.sharedMesh = meshExportData._meshes[i]; } else { SkinnedMeshRenderer skinnedRendererOrgin; if (index2SkinnedRenderer.TryGetValue(i, out skinnedRendererOrgin)) { skinnedRendererOrgin.sharedMesh = meshExportData._meshes[i]; } } } UtilityFuncs.MarkCurrentSceneIsDirty(); } Debug.Log("成功导出Mesh数据到" + path, meshExportData); } else { EditorUtility.DisplayDialog("提示", "没有导出任何Mesh数据!", "OK"); } } }