Unity——Multiple objects randomly change the angle and size within a certain range

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right

Table of contents

Article directory

1. Rendering

2. Use steps

1. Create a C# file in Assets

2. Select the desired multiple objects, right mouse button to start

3. Selected value range

4. Click to change the angle or change the size

Summarize

1. Rendering

before use:

After use:

2. Use steps

1. Create a C# file in Assets

code show as below:

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEditor;



public class Rotation : EditorWindow
{
    private static int hasRunCount = 0;

    [MenuItem("GameObject/随机旋转大小")]
    public static void ShowWindow()
    {
        int count = Selection.count;
        if (count < 1) return;
        hasRunCount++;
        Debug.Log(hasRunCount);
        if (hasRunCount == count)
        {
            Rotation window = EditorWindow.CreateWindow<Rotation>("TestWindow");
            window.position = new Rect(100, 100, 500, 200);//窗口大小
            hasRunCount = 0;
        }
    }

    private string m_tag;
    private Vector2 scrollViewPos;

    public object[] objs;
    public GameObject selectObj;
    private GameObject gameObject;
    private float value;
    private float value2;




    private void OnGUI()
    {
       
        gameObject = EditorGUI.ObjectField(new Rect(0, 0, 400, 18), gameObject, typeof(GameObject), true) as GameObject;
        value = EditorGUI.Slider(new Rect(10, 150, 380, 20), value, 0, 180);
        value2 = EditorGUI.Slider(new Rect(10, 175, 380, 20), value2, 0, 1);//滚动条位置大小和值域
        GUILayout.BeginHorizontal();
      
        float width = 500;
        float height = 530;

        Rect rect = new Rect(0, 0, width, height);

        GUILayout.BeginArea(rect);
        GUI.Box(rect, "");
        scrollViewPos = GUILayout.BeginScrollView(scrollViewPos, false, true, GUILayout.Height(height));
        GUILayout.BeginVertical();
       
       


        GUILayout.EndVertical();
        GUILayout.EndScrollView();
        GUILayout.EndArea();

        if (GUI.Button(new Rect(410, 150, 70, 20), "改变角度"))
        {
            CudeRot();
        }
        if (GUI.Button(new Rect(410, 175, 70, 20), "改变大小"))
        {
            CudeRot2();
        }
        GUILayout.EndHorizontal();

    }




    private void RandomRot(Transform game)
    {
        float val = value;
        float ran = UnityEngine.Random.Range(-val, val);

        if (game != null)
            game.Rotate(Vector3.up,ran);

    }

    private void RandomRot2(Transform game)
    {
        float val = value2;
        float ran = UnityEngine.Random.Range(-val, val);
        if (game != null)
          game.transform.localScale +=Vector3.one*ran;
 

    }
    
    public void CudeRot()
    {
        Transform[] trans = Selection.GetFiltered<Transform>(SelectionMode.TopLevel);
        if (trans.Length < 1) return;
        for (int i = 0; i < trans.Length; i++)
        {
            RandomRot(trans[i]);
        }
    }

    public void CudeRot2()
    {
        Transform[] trans = Selection.GetFiltered<Transform>(SelectionMode.TopLevel);
        if (trans.Length < 1) return;
        for (int i = 0; i < trans.Length; i++)
        {
            RandomRot2(trans[i]);
        }
    }
}

2. Select the desired multiple objects, right mouse button to start

3. Selected value range

4. Click to change the angle or change the size


Summarize

The above is a small plugin for unity I made, thanks for your support.

Guess you like

Origin blog.csdn.net/weixin_45522775/article/details/128692547