Unity——多个物体在一定范围内随机改变角度和大小

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

文章目录

一、效果图

二、使用步骤

1.在Assets里创建C#文件

2.选中所需的多个物体,鼠标右键启动

3.选定值域

4.点击改变角度或改变大小

总结

一、效果图

使用前:

使用后:

二、使用步骤

1.在Assets里创建C#文件

代码如下:

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.选中所需的多个物体,鼠标右键启动

3.选定值域

4.点击改变角度或改变大小


总结

以上就是我做的一个unity小插件,感谢支持。

猜你喜欢

转载自blog.csdn.net/weixin_45522775/article/details/128692547