Пример #1
0
 private void TransformObjectsBy(Transform[] objects)
 {
     foreach (var transform in objects)
     {
         Undo.RecordObject(transform, "Transformed object with offset");
         offset.AddTo(transform);
     }
 }
Пример #2
0
        public void ReplaceObjectsWithPrefab(GameObject[] objects, GameObject sample, TransformValues offset)
        {
            Selection.objects = objects.Select(ReplaceWithPrefab).ToArray();

            GameObject ReplaceWithPrefab(GameObject obj)
            {
                var hierarchyIndex = obj.transform.GetSiblingIndex();

                GameObject newObj = InstantiateSample(obj.transform.parent);

                Undo.RegisterCreatedObjectUndo(newObj, "Instantiated Prefab");

                SetupTransformValues();

                Undo.DestroyObjectImmediate(obj);
                newObj.transform.SetSiblingIndex(hierarchyIndex);
                return(newObj);

                void SetupTransformValues()
                {
                    newObj.transform.localPosition    = GetNewTransformationValue(_positionMod, obj.transform.localPosition, newObj.transform.localPosition);
                    newObj.transform.localEulerAngles = GetNewTransformationValue(_rotationMod, obj.transform.localEulerAngles, newObj.transform.localEulerAngles);
                    newObj.transform.localScale       = GetNewTransformationValue(_scaleMod, obj.transform.localScale, newObj.transform.localScale);

                    offset.AddTo(newObj.transform);
                }

                Vector3 GetNewTransformationValue(TransformOperation transformation, Vector3 oldValue, Vector3 newValue)
                {
                    switch (transformation)
                    {
                    default:
                    case TransformOperation.UseOldValue:
                    {
                        return(oldValue);
                    }

                    case TransformOperation.UseNewValue:
                    {
                        return(newValue);
                    }

                    case TransformOperation.AddNewValue:
                    {
                        return(oldValue + newValue);
                    }
                    }
                }
            }
        }