private static void RebuildProBuilderMesh(pb_Object pb) { try { GameObject go = pb.gameObject; pb.dontDestroyMeshOnDelete = true; Undo.DestroyObjectImmediate(pb); // don't delete pb_Entity here because it won't // actually get removed till the next frame, and // probuilderize wants to add it if it's missing // (which it looks like it is from c# side but // is not) pb = Undo.AddComponent <pb_Object>(go); pbMeshOps.ResetPbObjectWithMeshFilter(pb, true); pb.ToMesh(); pb.Refresh(); pb.Optimize(); } catch (System.Exception e) { Debug.LogError("Failed rebuilding ProBuilder mesh: " + e.ToString()); } }
private static void DoUpgrade(pb_Object[] all) { bool interactive = all != null && all.Length > 8; for (int i = 0; i < all.Length; i++) { pb_Object pb = all[i]; if (interactive) { EditorUtility.DisplayProgressBar( "Applying Materials", "Setting pb_Object " + all[i].id + ".", ((float)i / all.Length)); } pb.SetFaceMaterial(pb.faces, pb.gameObject.GetComponent <MeshRenderer>().sharedMaterial); pb.ToMesh(); pb.Refresh(); pb.Optimize(); } if (interactive) { EditorUtility.ClearProgressBar(); } }
/** * \brief Rebuild targets if they can't be refreshed. */ private static void StripAndProBuilderize(pb_Object[] targets, bool interactive = true) { for (int i = 0; i < targets.Length; i++) { if (interactive) { EditorUtility.DisplayProgressBar( "Refreshing ProBuilder Objects", "Reshaping pb_Object " + targets[i].id + ".", ((float)i / targets.Length)); } pb_Object pb = targets[i]; try { pb.ToMesh(); pb.Refresh(); pb.Optimize(); } catch { if (pb.msh != null) { RebuildProBuilderMesh(pb); } } } if (interactive) { EditorUtility.ClearProgressBar(); EditorUtility.DisplayDialog("Rebuild ProBuilder Objects", "Successfully rebuilt " + targets.Length + " ProBuilder Objects", "Okay"); } }
/** * \brief Rebuild targets if they can't be refreshed. */ private static void RebuildSharedIndices(pb_Object[] targets, bool interactive = true) { for (int i = 0; i < targets.Length; i++) { if (interactive) { EditorUtility.DisplayProgressBar( "Refreshing ProBuilder Objects", "Reshaping pb_Object " + targets[i].id + ".", ((float)i / targets.Length)); } pb_Object pb = targets[i]; try { pb.SetSharedIndices(pb_IntArrayUtility.ExtractSharedIndices(pb.vertices)); pb.ToMesh(); pb.Refresh(); pb.Optimize(); } catch (System.Exception e) { Debug.LogError("Failed rebuilding " + pb.name + " shared indices cache.\n" + e.ToString()); } } if (interactive) { EditorUtility.ClearProgressBar(); EditorUtility.DisplayDialog("Rebuild Shared Index Cache", "Successfully rebuilt " + targets.Length + " shared index caches", "Okay"); } }
/** * Do the thing. Return a pb_ActionResult indicating the success/failure of action. */ public override pb_ActionResult DoAction() { #if !UNITY_4_6 && !UNITY_4_7 ShadowCastingMode shadowMode = (ShadowCastingMode)pb_PreferencesInternal.GetInt("pb_CreateShadowObject_shadowMode", (int)ShadowCastingMode.ShadowsOnly); #endif float extrudeDistance = pb_PreferencesInternal.GetFloat("pb_CreateShadowObject_volumeSize", .08f); ExtrudeMethod extrudeMethod = (ExtrudeMethod)pb_PreferencesInternal.GetInt("pb_CreateShadowObject_extrudeMethod", (int)ExtrudeMethod.FaceNormal); foreach (pb_Object pb in selection) { pb_Object shadow = GetShadowObject(pb); if (shadow == null) { continue; } foreach (pb_Face f in shadow.faces) { f.ReverseIndices(); f.manualUV = true; } shadow.Extrude(shadow.faces, extrudeMethod, extrudeDistance); shadow.ToMesh(); shadow.Refresh(); shadow.Optimize(); #if !UNITY_4_6 && !UNITY_4_7 MeshRenderer mr = shadow.gameObject.GetComponent <MeshRenderer>(); mr.shadowCastingMode = shadowMode; if (shadowMode == ShadowCastingMode.ShadowsOnly) { mr.receiveShadows = false; } #endif Collider collider = shadow.GetComponent <Collider>(); while (collider != null) { GameObject.DestroyImmediate(collider); collider = shadow.GetComponent <Collider>(); } } // This is necessary! Otherwise the pb_Editor will be working with caches from // outdated meshes and throw errors. pb_Editor.Refresh(); return(new pb_ActionResult(Status.Success, "Create Shadow Object")); }
private static void OnFaceChanged(pb_Object pb) { pb.ToMesh(); pb.Refresh(); pb.Optimize(); // StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags( pb.gameObject ); // // if nodraw not found, and entity type should be batching static // if(pb.GetComponent<pb_Entity>().entityType != EntityType.Mover) // { // flags = flags | StaticEditorFlags.BatchingStatic; // GameObjectUtility.SetStaticEditorFlags(pb.gameObject, flags); // } }
/** * Ensure that this object has a valid mesh reference, and the geometry is * current. */ public static MeshRebuildReason VerifyMesh(pb_Object pb) { Mesh oldMesh = pb.msh; MeshRebuildReason reason = pb.Verify(); if (reason != MeshRebuildReason.None) { /** * If the mesh ID doesn't match the gameObject Id, it could mean two things - * 1. The object was just duplicated, and then made unique * 2. The scene was reloaded, and gameObject ids were recalculated. * If the latter, we need to clean up the old mesh. If the former, * the old mesh needs to *not* be destroyed. */ int meshNo = -1; if (oldMesh) { int.TryParse(oldMesh.name.Replace("pb_Mesh", ""), out meshNo); GameObject go = null; Object dup = EditorUtility.InstanceIDToObject(meshNo); try { go = (GameObject)dup; } catch (System.Exception e) {} if (go == null) { GameObject.DestroyImmediate(oldMesh); } } else { if (pb_Editor_Utility.IsPrefabRoot(pb.gameObject)) { pb.msh.hideFlags = (HideFlags)(1 | 2 | 4 | 8); } } pb.Optimize(); #if UNITY_5 EditorUtility.UnloadUnusedAssetsImmediate(); #else EditorUtility.UnloadUnusedAssets(); #endif } return(reason); }
public override void OnInspectorGUI() { GUI.backgroundColor = Color.green; if (GUILayout.Button("Open " + pb_Constant.PRODUCT_NAME)) { pb_Editor.MenuOpenWindow(); } GUI.backgroundColor = Color.white; if (!ren) { return; } Vector3 sz = ren.bounds.size; EditorGUILayout.Vector3Field("Object Size (read only)", sz); if (pb == null) { return; } if (pb.SelectedTriangles.Length > 0) { GUILayout.Space(5); offset = EditorGUILayout.Vector3Field("Quick Offset", offset); if (GUILayout.Button("Apply Offset")) { pbUndo.RecordObject(pb, "Offset Vertices"); pb.ToMesh(); pb.TranslateVertices_World(pb.SelectedTriangles, offset); pb.Refresh(); pb.Optimize(); if (editor != null) { editor.UpdateSelection(); } } } }
public static void InitObjectFlags(pb_Object pb, ColliderType colliderType, EntityType entityType) { switch (colliderType) { case ColliderType.BoxCollider: pb.gameObject.AddComponent <BoxCollider>(); break; case ColliderType.MeshCollider: pb.gameObject.AddComponent <MeshCollider>().convex = EditorPrefs.HasKey(pb_Constant.pbForceConvex) ? EditorPrefs.GetBool(pb_Constant.pbForceConvex) : false; break; } pb_Lightmap_Editor.SetObjectUnwrapParamsToDefault(pb); pb_Editor_Utility.SetEntityType(entityType, pb.gameObject); pb_Editor_Utility.ScreenCenter(pb.gameObject); pb.Optimize(); }
private static void OnFaceChanged( pb_Object pb ) { pb.ToMesh(); pb.Refresh(); pb.Optimize(); // StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags( pb.gameObject ); // // if nodraw not found, and entity type should be batching static // if(pb.GetComponent<pb_Entity>().entityType != EntityType.Mover) // { // flags = flags | StaticEditorFlags.BatchingStatic; // GameObjectUtility.SetStaticEditorFlags(pb.gameObject, flags); // } }
public override void OnInspectorGUI() { if (pb == null) { return; } if (ent == null) { return; } EntityType et = ent.entityType; et = (EntityType)EditorGUILayout.EnumPopup("Entity Type", et); if (et != ent.entityType) { pbUndo.RecordObjects(new Object[] { ent, ent.gameObject.GetComponent <pb_Object>() }, "Set Entity Type"); pb_Editor_Utility.SetEntityType(et, ent.gameObject); pb.ToMesh(); pb.Refresh(); pb.Optimize(); } GUILayout.Space(4); pb.userCollisions = EditorGUILayout.Toggle("Custom Collider", pb.userCollisions); // Convience if (pb.userCollisions) { GUI.enabled = false; } GUILayout.Label("Add Collider", EditorStyles.boldLabel); GUILayout.BeginHorizontal(); if (GUILayout.Button("Mesh Collider", EditorStyles.miniButtonLeft)) { EditorApplication.delayCall += AddMeshCollider; } if (GUILayout.Button("Box Collider", EditorStyles.miniButtonMid)) { EditorApplication.delayCall += AddBoxCollider; } if (GUILayout.Button("Remove Collider", EditorStyles.miniButtonRight)) { EditorApplication.delayCall += RemoveColliders; } GUILayout.EndHorizontal(); GUI.enabled = true; // GUILayout.Space(4); // if(GUI.changed) // EditorUtility.SetDirty(ent); }
/** * return true if shortcut should eat the event */ internal bool ClickShortcutCheck(pb_Object pb, pb_Face selectedFace) { Event e = Event.current; // Copy UV settings if(e.modifiers == (EventModifiers.Control | EventModifiers.Shift)) { // get first selected Auto UV face pb_Object firstObj; pb_Face source; pb_Editor.instance.GetFirstSelectedFace(out firstObj, out source); if( source != null ) { pbUndo.RecordObject(pb, "Copy UV Settings"); selectedFace.SetUV( new pb_UV(source.uv) ); selectedFace.SetMaterial( source.material ); pb_Editor_Utility.ShowNotification("Copy UV Settings"); pb.ToMesh(); pb.Refresh(); pb.Optimize(); RefreshUVCoordinates(); Repaint(); return true; } else { return false; } } else if(e.modifiers == EventModifiers.Control) { int len = pb.SelectedFaces == null ? 0 : pb.SelectedFaces.Length; if(len < 1) return false; pb_Face anchor = pb.SelectedFaces[len-1]; if(anchor == selectedFace) return false; pbUndo.RecordObject(pb, "AutoStitch"); pb.ToMesh(); bool success = pbUVOps.AutoStitch(pb, anchor, selectedFace); if(success) { RefreshElementGroups(pb); pb.SetSelectedFaces(new pb_Face[]{selectedFace}); // // only need to do this for one pb_Object... // for(int i = 0; i < selection.Length; i++) // selection[i].RefreshUV( editor.SelectedFacesInEditZone[i] ); pb.Refresh(); pb.Optimize(); SetSelectedUVsWithSceneView(); RefreshUVCoordinates(); pb_Editor_Utility.ShowNotification("Autostitch"); if(editor != null) editor.UpdateSelection(false); Repaint(); } else { pb.Refresh(); pb.Optimize(); } return success; } return false; }