画圆(颜色渐变的光点图)

首先,此处画的圆是由等距的圆点拼成的。项目所需,画了9圈,根据一个值改变颜色。

整体代码如下:

using System.Collections;

using UnityEngine;

public class LightSpots: MonoBehaviour {

    public GameObject circleModel;//要排布的物体
    public float changeAngle = 0;//旋转改变的角度

    private int count;//旋转一周需要的预制物体个数
    private float angle = 0;//当前角度
    private float r;//半径

    private Color colorRed;//红色
    private Color colorYellow;//黄色
    private Color colorBlue;//蓝色
    private Color colorWhite;//浅蓝

    private void Awake()
    {
        colorRed = Color.red;
        colorYellow = Color.yellow;
        colorBlue = Color.blue;
        colorWhite = Color.white;
    }

    // Use this for initialization
    private void Start()
    {
        InitSpot();
        ChangeColor(0.08f);
    }

    /// 
    /// 初始化所有点
    /// 
    private void InitSpot()
    {
        for (r = 0.1f; r < 0.51f; r += 0.05f)
        {
            float changeAngle1 = changeAngle / r;
            count = (int)(360 / changeAngle1);
            for (int i = 0; i < count; i++)
            {
                Vector3 center = circleModel.transform.position;
                GameObject obj = (GameObject)Instantiate(circleModel);
                obj.transform.parent = gameObject.transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                float hudu = (angle / 180) * Mathf.PI;
                float xx = center.x + r * Mathf.Cos(hudu);
                float yy = center.y + r * Mathf.Sin(hudu);
                obj.transform.position = new Vector3(xx, yy, 0);
                obj.tag = "Lap" +((int)(r*20-1)).ToString();
                angle += changeAngle1;
            }
        }
    }

    /// 
    /// 更改颜色
    /// 
    public void ChangeColor(float diameter)
    {
        float m = 62831853 * diameter * diameter;
        float n = Mathf.Sqrt(1 + 1764 * diameter * diameter);
        float o = m / n;
        
        for (int i=36;i<=180;i+=18)
        {
            if (o / i > 2000)
                ColorControl(i, colorRed);
            else if (o / i > 1000)
                ColorControl(i, Color.Lerp(colorRed, colorYellow, 0.33f));
            else if (o / i > 600)
                ColorControl(i, Color.Lerp(colorRed, colorYellow, 0.67f));
            else if (o / i > 400)
                ColorControl(i, colorYellow);
            else if (o / i > 300)
                ColorControl(i, Color.Lerp(colorYellow, colorBlue, 0.33f));
            else if (o / i > 200)
                ColorControl(i, Color.Lerp(colorYellow, colorBlue, 0.67f));
            else if (o / i > 100)
                ColorControl(i, colorBlue);
            else if (o / i > 50)
                ColorControl(i, Color.Lerp(colorBlue, colorWhite, 0.05f));
            else if (o / i >= 0)
                ColorControl(i, Color.Lerp(colorBlue, colorWhite, 0.1f));
            print(o / i);
        }
        
    }

    /// 
    /// 控制每一圈的颜色
    /// 
    /// 
    /// 
    private void ColorControl(int radius,Color color)
    {
        for (int j = 0; j < GameObject.FindGameObjectsWithTag("Lap" + (radius/18-1).ToString()).Length; j++)
            GameObject.FindGameObjectsWithTag("Lap" + (radius / 18 - 1).ToString())[j].GetComponent().color = color;
    }
}
InitSpot函数初始化圆点,通过两个for循环,根据半径画出9个圆,个数以及半径可自行更改。

调用ChangeColor方法可改变颜色。

结果图如下(中间圆点忽略):

画圆(颜色渐变的光点图)_第1张图片

画圆(颜色渐变的光点图)_第2张图片

你可能感兴趣的:(Unity3D)