Advertisement
napland

CusomtTransformInspector Scale and Position

Aug 19th, 2016
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1.     private AxisFlag scaleAxisFlag;
  2.     private float minScale, maxScale;
  3.     private bool scaleSame = true;
  4.     private void RandomScaleInspector()
  5.     {
  6.         EditorGUILayout.LabelField("Random Scale (local)", EditorStyles.boldLabel, layoutMaxWidth);
  7.  
  8.         scaleAxisFlag = (AxisFlag)EditorGUILayout.EnumMaskField("Scale Axis", scaleAxisFlag, layoutMaxWidth);
  9.         scaleSame = EditorGUILayout.ToggleLeft("Scale same", scaleSame, layoutMaxWidth);
  10.  
  11.         minScale = EditorGUILayout.FloatField("Min:", minScale, layoutMaxWidth);
  12.         maxScale = EditorGUILayout.FloatField("Max", maxScale, layoutMaxWidth);
  13.  
  14.         Transform[] selectedTransforms = Selection.transforms;
  15.         string btnLabel = "Scale " + _transform.name;
  16.         if (selectedTransforms.Length > 1)
  17.             btnLabel = "Scale selection";
  18.  
  19.         if (Button(btnLabel))
  20.         {
  21.             RandomScale(scaleAxisFlag, selectedTransforms, scaleSame);
  22.         }
  23.     }
  24.  
  25.     private void RandomScale(AxisFlag axis, Transform[] selected , bool scaleSame)
  26.     {
  27.         for (int i = 0; i < selected.Length; i++)
  28.         {
  29.             Vector3 temp = selected[i].localScale;
  30.             Vector3 random = Vector3.zero;
  31.             if (scaleSame)
  32.             {
  33.                 float rdm = Random.Range(minScale, maxScale);
  34.                 random.x = rdm;
  35.                 random.y = rdm;
  36.                 random.z = rdm;
  37.             }
  38.             else
  39.             {
  40.                 random.x = Random.Range(minScale, maxScale);
  41.                 random.y = Random.Range(minScale, maxScale);
  42.                 random.z = Random.Range(minScale, maxScale);
  43.             }
  44.  
  45.             if ((axis & AxisFlag.X) == AxisFlag.X)
  46.                 temp.x = random.x;
  47.  
  48.             if ((axis & AxisFlag.Y) == AxisFlag.Y)
  49.                 temp.y = random.y;
  50.  
  51.             if ((axis & AxisFlag.Z) == AxisFlag.Z)
  52.                 temp.z = random.z;
  53.  
  54.             Undo.RecordObject(_transform, "random scale " + selected[i].name);
  55.             selected[i].localScale = temp;
  56.         }
  57.     }
  58.  
  59.  
  60.     private Vector3 minPosition, maxPosition;
  61.     private void RandomPositionInspector()
  62.     {
  63.         EditorGUILayout.LabelField("Random Position", EditorStyles.boldLabel, layoutMaxWidth);
  64.         minPosition = EditorGUILayout.Vector3Field("Min", minPosition, layoutMaxWidth);
  65.         maxPosition = EditorGUILayout.Vector3Field("Max", maxPosition, layoutMaxWidth);
  66.  
  67.         Transform[] selectedTransforms = Selection.transforms;
  68.         string btnLabel = "Move " + _transform.name;
  69.         if (selectedTransforms.Length > 1)
  70.             btnLabel = "Move selection";
  71.  
  72.         if (Button(btnLabel))
  73.         {
  74.             RandomPosition(minPosition, maxPosition, selectedTransforms);
  75.         }
  76.     }
  77.  
  78.    
  79.  
  80.     private void RandomPosition(Vector3 min , Vector3 max, Transform[] t)
  81.     {
  82.         for (int i = 0; i < t.Length; i++)
  83.         {
  84.             Vector3 temp = t[i].position;
  85.             if (!Mathf.Approximately(min.x, max.x))
  86.                 temp.x = Random.Range(min.x, max.x);
  87.  
  88.             if (!Mathf.Approximately(min.y, max.y))
  89.                 temp.y = Random.Range(min.y, max.y);
  90.  
  91.             if (!Mathf.Approximately(min.z, max.z))
  92.                 temp.z = Random.Range(min.z, max.z);
  93.  
  94.             Undo.RecordObject(t[i], "Random position " + t[i].name);
  95.             t[i].position = temp;
  96.         }
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement