2016년 11월 10일 목요일

[C#] 유니티 그래프 스크립트 ([C#]Graph Script at Unity)


So here is the script.

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

public class BarGraphManager : MonoBehaviour {

    public GameObject linerenderer;
    public GameObject pointer;

    public GameObject HolderPrefb;

    public GameObject holder;

    public Material mat;

    public Text topValue;

    public List<GraphDatagraphData = new List<GraphData>();

    private GraphData gd;
    private float highestValue;

    void Start(){
        UpdateData();
    }
    
    public void UpdateData() {

        // Instantiate an empty gameObject to hold all line renderers
        holder = Instantiate(HolderPrefb,Vector3.zero,Quaternion.identityas GameObject;
        holder.name = "h2";

        graphData.Clear();

        // Lenght of data to be shown in graph
        int length = Random.Range(5,12);
        // Inserting data to the list
        for(int i = 0i < lengthi++)
        {
            gd = new GraphData();

            gd.income = Random.Range(10.0f,100.0f);

            if(gd.income > highestValue)
                highestValue = gd.income;

            graphData.Add(gd);
        }

        // Adjusting value to fit in graph
        for(int i = 0i < graphData.Counti++)
        {
            // since Y axis is from 0 to 3 we are dividing the income with the highest income
            // so that we get a value less than or equals to 1 and than we can multiply that
            // number with Y axis range to fit in graph
            // e.gincome = 90highest = 90 so 90/90 = 1 and than 1*3 = 3 so for 90Y = 3
            graphData[i].income = (graphData[i].income/highestValue)*3;
        }

        topValue.text = Mathf.FloorToInt(highestValue).ToString();

        StartCoroutine("BarGraph",graphData);
    }

    IEnumerator BarGraph(List<GraphDatagd)
    {

        float gap = 1;

        // X axis starts from 0 to 4 so according to my logic i have to check if noof data
        // is > 4 than we should divide the lenght of x axis with the data countthis will
        // give us the gap between bars that we need to keep.

        if(gd.Count > 4)
        {
            gap = 4.0f/gd.Count;
        }

        float xIncrement = gap;
        int dataCount = 0;
        Vector3 startpoint = Vector3.zero;

        while(dataCount < gd.Count)
        {

            Vector3 endpoint = new Vector3(xIncrement,gd[dataCount].income,0.96f);
            startpoint = new Vector3(endpoint.x,0,0.96f);
            // pointer is an empty gameObjecti made a prefab of it and attach it in the inspector
            GameObject p = Instantiate(pointernew Vector3(startpoint.xstartpoint.y0.97f),Quaternion.identityas GameObject;
            p.transform.parent = holder.transform;

            // linerenderer is an empty gameObject with Line Renderer Component Attach to it
            // i made a prefab of it and attach it in the inspector
            GameObject lineObj = Instantiate(linerenderer,transform.position,Quaternion.identityas GameObject;
            lineObj.transform.parent = holder.transform;
            lineObj.name = dataCount.ToString();

            LineRenderer lineRenderer = lineObj.GetComponent<LineRenderer>();
            
            lineRenderer.material = mat;
            lineRenderer.SetWidth(0.15F0.15F);
            lineRenderer.SetVertexCount(2);

            while(p.transform.position.y < endpoint.y)
            {
                p.transform.Translate(Vector3.up*Time.deltaTime*8Space.World);
                
                lineRenderer.SetPosition(0startpoint);
                lineRenderer.SetPosition(1p.transform.position);
                
                yield return null;
            }

            lineRenderer.SetPosition(0startpoint);
            lineRenderer.SetPosition(1endpoint);

            
            p.transform.position = endpoint;

            startpoint = endpoint;
            dataCount++;
            xIncrement+= gap;

            yield return null;

        }
    }



    public class GraphData
    {
        public float income;
    }
}


출처 : http://cafe.naver.com/unityhub

댓글 없음:

댓글 쓰기

아이디어 및 질문 외에 댓글은 사양합니다.