首页 > 解决方案 > 如何创建一个管理器脚本来一次控制所有绘制的圆圈?

问题描述

第一个脚本用于绘制圆圈:当我将此脚本附加到游戏对象时,它会在对象周围绘制一个圆圈。

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

[RequireComponent(typeof(LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 5;
    [Range(1, 50)]
    public float yradius = 5;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;

    private LineRenderer line;

    void Start()
    {
        line = gameObject.GetComponent<LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
    }

    void Update()
    {
        line.startWidth = lineThickness;
        line.endWidth = lineThickness;

        CreatePoints();
    }

    void CreatePoints()
    {
        float x;
        float z;

        float angle = 20;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, height, z));

            angle += (360f / segments + 1);
        }
    }
}

现在我创建了一个管理器脚本,它应该同时控制所有的圈子:

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

public class CirclesManager : MonoBehaviour
{
    public GameObject[] objectsToAddCircles;
    [Range(0, 50)]
    public int segments = 50;
    [Range(1, 50)]
    public float xradius = 5;
    [Range(1, 50)]
    public float yradius = 5;
    [Range(-10, 10)]
    public float height = 0;
    public bool changeBothRadius = false;
    [Range(0.1f, 2)]
    public float lineThickness = 0.1f;
    public bool minimumRadius = false;

    void Start()
    {

        for (int i = 0; i < objectsToAddCircles.Length; i++)
        {
            objectsToAddCircles[i].AddComponent<DrawCircle>();
            objectsToAddCircles[i].AddComponent<LineRenderer>();
        }
    }

    void Update()
    {
        for (int i = 0; i < objectsToAddCircles.Length; i++)
        {
            var lr = objectsToAddCircles[i].GetComponent<LineRenderer>();
            lr.startWidth = lineThickness;
            lr.endWidth = lineThickness;

            var dc = objectsToAddCircles[i].GetComponent<DrawCircle>();
            dc.segments = segments;
            dc.xradius = xradius;
            dc.yradius = yradius;
            dc.height = height;
            dc.changeBothRadius = changeBothRadius;
            dc.minimumRadius = minimumRadius;
        }
    }
}

所以现在每个对象都有 LineRenderer 组件和 DrawCircle 脚本,现在 CirclesManager 脚本可以正常处理所有对象,但是如果我尝试更改单个对象设置,它不会改变。例如,我可以在管理器脚本中更改 xrdaius 或 yradius 滑块,但如果我尝试在特定对象中更改它们,滑块不会移动也不会改变。

无法弄清楚为什么管理器脚本正在工作,但不是每个带有脚本和 LineRenderer 的单独对象。

标签: c#unity3d

解决方案


在 Update 的 CirclesManager 类中,您有以下几行:

dc.xradius = xradius;
dc.yradius = yradius;

无论您在何处以及如何更改单个DrawCircle实例半径,这些行都会覆盖这些值。

我不知道你想要归档什么样的行为,但你可以创建一个 bool 数组,这样你就可以手动设置哪些圆圈将由 CirclesManager 驱动,哪些圆圈将使用它们自己的值:

// you can change it in the inspector which is handy
// if i'th value of this array is false
// then i'th CircleDrawer GameObject in objectsToAddCircles array
// won't be affected by this manager
public bool changableCircle[];

void Start() {
    // your code
    changableCircle = new bool[objectsToAddCircles.Length];
}

void Update() {
    for(...) {
        // values which are always overwritten by manager
        if(changableCircle[i]) {
            // values which you don't want to be changed by this manager
        }
    }
}

推荐阅读