简单的slider拖拽控制

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class transformCtrl : MonoBehaviour {

    public float moveSpeed;
    public float sensitivityMouse;

    public Transform root;
    public Slider[] position;
    public Slider[] rotation;
    public Slider scale;
    public float timer;
    public bool isRote;

    private void Start()
    {
        for(int i = 0;i < position.Length; i++)
        {
            int num = i;
            rotation[i].onValueChanged.AddListener((float value) => sliderChange());
            position[i].onValueChanged.AddListener((float value) => sliderChange());
        }
        scale.onValueChanged.AddListener((float value) => sliderChange());
    }
    // Update is called once per frame
    void Update () {

        //控制旋转
        float rx = Input.GetAxis("rotationX");
        float ry = Input.GetAxis("rotationY");
        float rz = Input.GetAxis("rotationZ");

        //控制位移
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float z = Input.GetAxis("moveZ");

        //控制缩放
        float s = Input.GetAxis("carScale");

        transform.Translate(z / moveSpeed, v / moveSpeed, h / moveSpeed);
        transform.Rotate(rx, ry, rz);
        transform.localScale = new Vector3(transform.localScale.x + s/moveSpeed, transform.localScale.y + s/moveSpeed, transform.localScale.z + s/moveSpeed);
        if (Input.GetMouseButton(1))
        {
            transform.Rotate(-Input.GetAxis("Mouse Y") * sensitivityMouse, Input.GetAxis("Mouse X") * sensitivityMouse, 0);
        }

        if (Input.GetMouseButton(0))
        {
            if (isRote == true)
            {
                root.Rotate(new Vector3(rotation[0].value, rotation[1].value, rotation[2].value));
                root.localScale = new Vector3(scale.value + root.localScale.x, scale.value + root.localScale.y, scale.value + root.localScale.z);
                root.localPosition = new Vector3(position[0].value + root.localPosition.x, position[1].value + root.localPosition.y, position[2].value + root.localPosition.z);
            }
        }
        else
        {
            isRote = false;
            scale.value = 0;
            rotation[0].value = rotation[1].value = rotation[2].value = 0;
            position[0].value = position[1].value = position[2].value = 0;
        }
    }

    public void sliderChange()
    {
        isRote = true;
    }

    public void resetPos(string pos)
    {
        Vector3 localPos = transform.localEulerAngles;
        if(pos == "x")
        {
            position[0].value = 0;
        }
        if(pos == "y")
        {
            position[1].value = 0;
        }

        if(pos == "z")
        {
            position[2].value = 0;
        }
        if (pos == "rx")
        {
            localPos.x = 0;
            transform.localEulerAngles = localPos;
        }
        if (pos == "ry")
        {
            localPos.y = 0;
            transform.localEulerAngles = localPos;
        }

        if (pos == "rz")
        {
            localPos.z = 0;
            transform.localEulerAngles = localPos;
        }

    }
}

 

你可能感兴趣的:(unity)