internal static void CreateRectangularTilemap() { GameObject gameObject = TilemapEditor.FindOrCreateRootGrid(); string uniqueNameForSibling = GameObjectUtility.GetUniqueNameForSibling(gameObject.transform, "Tilemap"); GameObject gameObject2 = ObjectFactory.CreateGameObject(uniqueNameForSibling, new Type[] { typeof(Tilemap), typeof(TilemapRenderer) }); Undo.SetTransformParent(gameObject2.transform, gameObject.transform, ""); gameObject2.transform.position = Vector3.zero; Undo.SetCurrentGroupName("Create Tilemap"); }
internal static void Place(GameObject go, GameObject parent, bool ignoreSceneViewPosition = true) { Transform defaultObjectTransform = SceneView.GetDefaultParentObjectIfSet(); if (parent != null) { // At this point, RecordStructureChange is already ongoing (from the CreatePrimitive call through the CreateAndPlacePrimitive method). We need to flush the stack to finalise the RecordStructureChange before the // following SetTransformParent call takes place. Undo.FlushTrackedObjects(); SetGameObjectParent(go, parent.transform); } else if (defaultObjectTransform != null) { // At this point, RecordStructureChange is already ongoing (from the CreatePrimitive call through the CreateAndPlacePrimitive method). We need to flush the stack to finalise the RecordStructureChange before the // following SetTransformParent call takes place. Undo.FlushTrackedObjects(); SetGameObjectParent(go, defaultObjectTransform); } else { // When creating a 3D object without a parent, this option puts it at the world origin instead of scene pivot. if (placeObjectsAtWorldOrigin) { go.transform.position = Vector3.zero; } else if (ignoreSceneViewPosition) { SceneView.PlaceGameObjectInFrontOfSceneView(go); } StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent } // Only at this point do we know the actual parent of the object and can modify its name accordingly. GameObjectUtility.EnsureUniqueNameForSibling(go); Undo.SetCurrentGroupName("Create " + go.name); var sh = SceneHierarchyWindow.GetSceneHierarchyWindowToFocusForNewGameObjects(); if (sh != null) { sh.Focus(); } Selection.activeGameObject = go; }
public static void SetPositionDelta(Vector3 newPosition, Vector3 oldPosition) { if (s_MouseDownState == null) { return; } s_PreviousHandlePosition = newPosition; Vector3 positionDelta = newPosition - oldPosition; Object[] undoObjects = new Object[s_MouseDownState.Length]; string undoName = ""; for (int i = 0; i < s_MouseDownState.Length; i++) { var cur = s_MouseDownState[i]; undoObjects[i] = (cur.rectTransform != null ? (Object)cur.rectTransform : (Object)cur.transform); undoName = (cur.rectTransform != null ? cur.rectTransform.name : cur.transform.name); } Undo.RecordObjects(undoObjects, "Move"); // The name here is less important, it has to be updated at the end of the move if (s_MouseDownState.Length > 0) { s_MouseDownState[0].SetPositionDelta(positionDelta, true); Vector3 firstDelta = s_MouseDownState[0].transform.position - s_MouseDownState[0].position; for (int i = 1; i < s_MouseDownState.Length; i++) { s_MouseDownState[i].SetPositionDelta(firstDelta, false); } if (undoObjects.Length == 1) { Undo.SetCurrentGroupName("Move " + undoName + newPosition); } else { Undo.SetCurrentGroupName("Move Selected Objects " + newPosition); } } }
public static void SetScaleDelta(Vector3 scaleDelta, Quaternion pivotRotation) { if (s_MouseDownState == null) { return; } SetLocalHandleOffsetScaleDelta(scaleDelta, pivotRotation); Object[] undoObjects = new Object[s_MouseDownState.Length]; string undoName = ""; for (int i = 0; i < s_MouseDownState.Length; i++) { var cur = s_MouseDownState[i]; undoObjects[i] = cur.transform; undoName = cur.transform.name; } Undo.RecordObjects(undoObjects, "Scale Selected Objects"); Vector3 point = Tools.handlePosition; for (int i = 0; i < s_MouseDownState.Length; i++) { // Scale about handlePosition or local pivot based on pivotMode if (Tools.pivotMode == PivotMode.Pivot) { point = s_MouseDownState[i].position; } if (individualSpace) { pivotRotation = s_MouseDownState[i].rotation; } s_MouseDownState[i].SetScaleDelta(scaleDelta, point, pivotRotation, false); } if (undoObjects.Length == 1) { Undo.SetCurrentGroupName("Scale " + undoName); } }
internal static void Place(GameObject go, GameObject parent) { if (parent != null) { var transform = go.transform; Undo.SetTransformParent(transform, parent.transform, "Reparenting"); transform.localPosition = Vector3.zero; transform.localRotation = Quaternion.identity; transform.localScale = Vector3.one; go.layer = parent.layer; if (parent.GetComponent <RectTransform>()) { ObjectFactory.AddComponent <RectTransform>(go); } } else { // When creating a 3D object without a parent, this option puts it at the world origin instead of scene pivot. if (EditorPrefs.GetBool("Create3DObject.PlaceAtWorldOrigin", false)) { go.transform.position = Vector3.zero; } else { SceneView.PlaceGameObjectInFrontOfSceneView(go); } StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent } // Only at this point do we know the actual parent of the object and can modify its name accordingly. GameObjectUtility.EnsureUniqueNameForSibling(go); Undo.SetCurrentGroupName("Create " + go.name); EditorWindow.FocusWindowIfItsOpen <SceneHierarchyWindow>(); Selection.activeGameObject = go; }
private static void CreateSpriteMaskGameObject() { GameObject gameObject = ObjectFactory.CreateGameObject("", new Type[] { typeof(SpriteMask) }); if (Selection.activeObject is Sprite) { gameObject.GetComponent <SpriteMask>().sprite = (Sprite)Selection.activeObject; } else if (Selection.activeObject is Texture2D) { string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!string.IsNullOrEmpty(assetPath)) { Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath); if (sprite != null) { gameObject.GetComponent <SpriteMask>().sprite = sprite; } } } else if (Selection.activeObject is GameObject) { GameObject gameObject2 = (GameObject)Selection.activeObject; PrefabType prefabType = PrefabUtility.GetPrefabType(gameObject2); if (prefabType != PrefabType.Prefab && prefabType != PrefabType.ModelPrefab) { GameObjectUtility.SetParentAndAlign(gameObject, gameObject2); } } gameObject.name = GameObjectUtility.GetUniqueNameForSibling(gameObject.transform.parent, SpriteMaskEditor.Contents.newSpriteMaskName.text); Undo.SetCurrentGroupName(SpriteMaskEditor.Contents.createSpriteMaskUndoString.text); Selection.activeGameObject = gameObject; }
internal static void RemoveTerrainLayer(Terrain terrain, int index) { var terrainData = terrain.terrainData; int width = terrainData.alphamapWidth; int height = terrainData.alphamapHeight; float[,,] alphamap = terrainData.GetAlphamaps(0, 0, width, height); int alphaCount = alphamap.GetLength(2); int newAlphaCount = alphaCount - 1; float[,,] newalphamap = new float[height, width, newAlphaCount]; // move further alphamaps one index below for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { for (int a = 0; a < index; ++a) { newalphamap[y, x, a] = alphamap[y, x, a]; } for (int a = index + 1; a < alphaCount; ++a) { newalphamap[y, x, a - 1] = alphamap[y, x, a]; } } } // normalize weights in new alpha map for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { float sum = 0.0F; for (int a = 0; a < newAlphaCount; ++a) { sum += newalphamap[y, x, a]; } if (sum >= 0.01) { float multiplier = 1.0F / sum; for (int a = 0; a < newAlphaCount; ++a) { newalphamap[y, x, a] *= multiplier; } } else { // in case all weights sum to pretty much zero (e.g. // removing splat that had 100% weight), assign // everything to 1st splat texture (just like // initial terrain). for (int a = 0; a < newAlphaCount; ++a) { newalphamap[y, x, a] = (a == 0) ? 1.0f : 0.0f; } } } } // remove splat from terrain prototypes var layers = terrainData.terrainLayers; var newSplats = new TerrainLayer[layers.Length - 1]; for (int a = 0; a < index; ++a) { newSplats[a] = layers[a]; } for (int a = index + 1; a < alphaCount; ++a) { newSplats[a - 1] = layers[a]; } Undo.SetCurrentGroupName("Remove terrain layer"); terrainData.SetTerrainLayersRegisterUndo(newSplats, "Remove Layer"); var undoObjects = new List <UnityEngine.Object>(); undoObjects.AddRange(terrainData.alphamapTextures); Undo.RegisterCompleteObjectUndo(undoObjects.ToArray(), "Apply Modified Alphamaps"); Undo.CollapseUndoOperations(Undo.GetCurrentGroup()); // set new alphamaps terrainData.SetAlphamaps(0, 0, newalphamap); }
public void RotationField(bool disabled) { Transform transform0 = targets[0] as Transform; Vector3 eulerAngles0 = transform0.GetLocalEulerAngles(transform0.rotationOrder); int differentRotationMask = 0b000; bool differentRotationOrder = false; for (int i = 1; i < targets.Length; i++) { Transform otherTransform = (targets[i] as Transform); if (differentRotationMask != 0b111) { Vector3 otherLocalEuler = otherTransform.GetLocalEulerAngles(otherTransform.rotationOrder); for (int j = 0; j < 3; j++) { if (otherLocalEuler[j] != eulerAngles0[j]) { differentRotationMask |= 1 << j; } } } differentRotationOrder |= otherTransform.rotationOrder != transform0.rotationOrder; } Rect r = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight * (EditorGUIUtility.wideMode ? 1 : 2)); GUIContent label = EditorGUI.BeginProperty(r, rotationContent, m_Rotation); m_EulerFloats[0].doubleVal = eulerAngles0.x; m_EulerFloats[1].doubleVal = eulerAngles0.y; m_EulerFloats[2].doubleVal = eulerAngles0.z; int id = GUIUtility.GetControlID(s_FoldoutHash, FocusType.Keyboard, r); if (AnimationMode.InAnimationMode() && transform0.rotationOrder != RotationOrder.OrderZXY) { string rotationLabel = differentRotationOrder ? "Mixed" : transform0.rotationOrder.ToString().Substring(RotationOrder.OrderXYZ.ToString().Length - 3); label.text = label.text + " (" + rotationLabel + ")"; } // Using manual 3 float fields here instead of MultiFloatField or Vector3Field // since we want to query expression validity of each individually, and // so that the label and the fields can be disabled separately, similar to // regular property fields. Also want to avoid superfluous label, which // creates a focus target even when there's no content (Case 953241). r = EditorGUI.MultiFieldPrefixLabel(r, id, label, 3); r.height = EditorGUIUtility.singleLineHeight; int eulerChangedMask = 0; using (new EditorGUI.DisabledScope(disabled)) { var eCount = m_EulerFloats.Length; float w = (r.width - (eCount - 1) * EditorGUI.kSpacingSubLabel) / eCount; Rect nr = new Rect(r) { width = w }; var prevWidth = EditorGUIUtility.labelWidth; var prevIndent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; for (int i = 0; i < m_EulerFloats.Length; i++) { EditorGUIUtility.labelWidth = EditorGUI.GetLabelWidth(s_XYZLabels[i]); EditorGUI.BeginChangeCheck(); EditorGUI.showMixedValue = (differentRotationMask & (1 << i)) != 0; EditorGUI.FloatField(nr, s_XYZLabels[i], ref m_EulerFloats[i]); if (EditorGUI.EndChangeCheck() && m_EulerFloats[i].hasResult) { eulerChangedMask |= 1 << i; } nr.x += w + EditorGUI.kSpacingSubLabel; } EditorGUIUtility.labelWidth = prevWidth; EditorGUI.indentLevel = prevIndent; } if (eulerChangedMask != 0) { eulerAngles0 = new Vector3( MathUtils.ClampToFloat(m_EulerFloats[0].doubleVal), MathUtils.ClampToFloat(m_EulerFloats[1].doubleVal), MathUtils.ClampToFloat(m_EulerFloats[2].doubleVal)); Undo.RecordObjects(targets, "Inspector"); // Generic undo title as remove duplicates will discard the name. Undo.SetCurrentGroupName(string.Format("Set Rotation")); for (var idx = 0; idx < targets.Length; ++idx) { var tr = targets[idx] as Transform; if (tr == null) { continue; } var trEuler = tr.GetLocalEulerAngles(tr.rotationOrder); // if we have any per-object expressions just entered, we need to evaluate // it for each object with their own individual input value for (int c = 0; c < 3; ++c) { if ((eulerChangedMask & (1 << c)) != 0) { if (m_EulerFloats[c].expression != null) { double trEulerComp = eulerAngles0[c]; if (m_EulerFloats[c].expression.Evaluate(ref trEulerComp, idx, targets.Length)) { trEuler[c] = MathUtils.ClampToFloat(trEulerComp); } } else { trEuler[c] = MathUtils.ClampToFloat(eulerAngles0[c]); } } } tr.SetLocalEulerAngles(trEuler, tr.rotationOrder); if (tr.parent != null) { tr.SendTransformChangedScale(); // force scale update, needed if tr has non-uniformly scaled parent. } } m_Rotation.serializedObject.SetIsDifferentCacheDirty(); } EditorGUI.showMixedValue = false; if (differentRotationOrder) { EditorGUILayout.HelpBox("Transforms have different rotation orders, keyframes saved will have the same value but not the same local rotation", MessageType.Warning); } EditorGUI.EndProperty(); }