【Unity】CSVファイルを読み込んで複数オブジェクトを移動させる

記事内に広告が含まれています。

備忘録です。

CubeSphere,CapsuleをCSVファイルの数値から動かす。

Assets>ResourcesにtestCSV.csvを入れる。

testCSV.csv
t[s],module,x,y,z,module,x,y,z,module,x,y,z
0,Cube,0,0,0,Sphere,0,0,0,Capsule,0,0,0
1,Cube,0,0,1,Sphere,0,0,0,Capsule,0,0,1
2,Cube,0,0,2,Sphere,0,0,0,Capsule,0,0,2
3,Cube,0,0,2,Sphere,0,0,4,Capsule,0,0,3
4,Cube,1,0,2,Sphere,0,0,4,Capsule,0,0,3
5,Cube,2,0,2,Sphere,0,0,4,Capsule,0,0,3
6,Cube,3,0,2,Sphere,0,0,4,Capsule,0,0,3
7,Cube,3,0,1,Sphere,0,0,0,Capsule,0,0,3
8,Cube,3,0,0,Sphere,0,0,0,Capsule,0,0,3
9,Cube,3,0,0,Sphere,0,1,0,Capsule,0,1,3
10,Cube,3,0,0,Sphere,0,2,0,Capsule,4,2,3
11,Cube,3,0,0,Sphere,0,3,0,Capsule,4,2,3
12,Cube,3,0,0,Sphere,0,3,0,Capsule,4,2,2
13,Cube,3,0,0,Sphere,0,3,0,Capsule,4,2,1
14,Cube,3,0,0,Sphere,1,3,0,Capsule,0,2,0
15,Cube,2,0,0,Sphere,2,3,0,Capsule,0,1,0
16,Cube,1,4,0,Sphere,2,3,0,Capsule,0,0,0
17,Cube,0,4,0,Sphere,2,3,0,Capsule,0,0,0
18,Cube,0,4,0,Sphere,2,3,0,Capsule,0,0,0
19,Cube,0,4,0,Sphere,2,3,0,Capsule,0,0,0
20,Cube,0,0,0,Sphere,1,2,0,Capsule,0,0,0

以下をGameObjectに追加。これが親オブジェクト。

CSVReader.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class CSVReader : MonoBehaviour {
    public int DateSize = 21;
    int i = 0;
    private float startTime,distance;
    float time = 1f;
    TextAsset csvFile;
    public List<string[]> csvDatas = new List<string[]>();
    void Start(){
        csvFile = Resources.Load("testCSV") as TextAsset;
        StringReader reader = new StringReader(csvFile.text);
        while (reader.Peek() != -1)
        {
            string line = reader.ReadLine();
            csvDatas.Add(line.Split(','));
        }
    }
    void Update(){//print
        time += Time.deltaTime;
        if(time>1){
            i++;
            if(i>DateSize)i=1;
            time = 0;
        }
        Debug.Log("t=" + csvDatas[i][0] + ",x=" + csvDatas[i][2] + ",y=" + csvDatas[i][3] + ",z=" + csvDatas[i][4]+ ",x=" + csvDatas[i][6] + ",y=" + csvDatas[i][7] + ",z=" + csvDatas[i][8]+ ",x=" + csvDatas[i][10] + ",y=" + csvDatas[i][11] + ",z=" + csvDatas[i][12]);
    }
}

以下を各オブジェクトに追加。これが子オブジェクト。

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

public class move_dt : MonoBehaviour{
    public int DateSize = 21;
    public int ModuleNumber = 3;
    float time = 1f;
    int i = 0;
    int j = 0;
    private float startTime,distance;
    private Vector3 startPosition, targetPosition;
    void Start(){
    }
    void Update(){
        float x,y,z;
        GameObject objParent = transform.parent.gameObject;
        CSVReader scParent = objParent.GetComponent<CSVReader>();
        for(j = 0;j<(ModuleNumber*4);j++){
            if(scParent.csvDatas[1][j] == name){
                break;
            }
        }
        time += Time.deltaTime;
        if(time >= 1){
            i ++;
            if(i>DateSize)i = 1;
            x = float.Parse(scParent.csvDatas[i][j+1]);
            y = float.Parse(scParent.csvDatas[i][j+2]);
            z = float.Parse(scParent.csvDatas[i][j+3]);
            startPosition = transform.position;
            targetPosition = new Vector3(x,y,z);
            time = 0;
        }
        transform.position = Vector3.Lerp(startPosition,targetPosition,time);
    }
}

追加で干渉時に色を変化

コメント