- Home /
The question is answered, right answer was accepted
How to make good parser to get colours from RGBa and set them to objects
Script changed a lot but what is clear now is the fact the source of most of problems was/is parser and conversion of strings to colors so also title was changed. Even ifelse loop doesn't work. I am running out of ideas.
Errors:
Assets/MainScript.js(51,48): BCE0022: Cannot convert 'String[]' to 'String'.
Assets/MainScript.js(54,42): BCE0017: The best overload for the method 'MainScript.ParseColor(String)' is not compatible with the argument list '(char)'.
Assets/MainScript.js(54,26): BCE0017: The best overload for the method 'UnityEngine.Color.set_Item(int, float)' is not compatible with the argument list '(int, UnityEngine.Color)'.
Assets/MainScript.js(54,26): BCE0049: Expression 'self.colorArray[i].Item[h]' cannot be assigned to.
Assets/MainScript.js(142,71): BCE0022: Cannot convert 'float' to 'UnityEngine.Color'.
#pragma strict
import System.IO;
//declare global variables
var colorArray : Color[];
var colorStringArray : String[];
var electrodes : GameObject[];
var n : int;
var myMaterial : Material;
var radius: int;
public function ParseColor (col : String) : Color
{
//Takes strings formatted with numbers and no spaces before or after the commas:
// "1.0,1.0,.35,1.0"
var strings = col.Split(","[0] );
var output : Color;
for (var i = 0; i < 4; i++)
{
output[i] = System.Single.Parse(strings[i]);
}
return output;
}
function Start () {
// Loading names of colours from .txt
colorArray = new Color[64];
for (var i = 0; i < 64; i++)
{
var txtname = "Assets\\\\colours_S001R01.edf_" + i + ".txt";
var sr : StreamReader = new System.IO.StreamReader(txtname);
while(!sr.EndOfStream)
{
var fromtxt : String;
try
{
fromtxt = sr.ReadLine();
if(fromtxt == null)
{
sr.Close();
}
}
catch (e)
{
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
colorStringArray[i] = fromtxt.Split(";"[0]);
}
for (var h = 0; h < 9600; h++){
colorArray[i][h] = ParseColor(colorStringArray[i][h]);}
}
// Electrodes are placed over the model of the brain in positions indicated in the picture given by EEG laboratory
for (var j = 0; i < 64; j++) {
electrodes[j]= GameObject.CreatePrimitive(PrimitiveType.Sphere);}
for (var k = 0; i < 64; k++) {
(electrodes[k].collider as SphereCollider).radius = 35;}
for (var l = 0; i < 64; l++) {
electrodes[l].renderer.material = myMaterial;}
for (var m = 0; i < 64; m++) {
electrodes[m].renderer.material.color = Color.grey;}
//positions
electrodes[1].transform.position = Vector3(0, 0, 400); //edit positions, F for float
electrodes[2].transform.position = Vector3(5, 1.5F, 8);
electrodes[3].transform.position = Vector3(6, 1.5F, 8);
electrodes[4].transform.position = Vector3(7, 1.5F, 8);
electrodes[5].transform.position = Vector3(8, 1.5F, 8);
electrodes[6].transform.position = Vector3(9, 1.5F, 8);
electrodes[7].transform.position = Vector3(10, 1.5F, 8);
electrodes[8].transform.position = Vector3(4, 1.5F, 7);
electrodes[9].transform.position = Vector3(5, 1.5F, 7);
electrodes[10].transform.position = Vector3(105, 0, 400);
electrodes[11].transform.position = Vector3(0, 0, 400);
electrodes[12].transform.position = Vector3(-105, 0, 400);
electrodes[13].transform.position = Vector3(9, 1.5F, 7);
electrodes[14].transform.position = Vector3(10, 1.5F, 7);
electrodes[15].transform.position = Vector3(4, 1.5F, 6);
electrodes[16].transform.position = Vector3(5, 1.5F, 6);
electrodes[17].transform.position = Vector3(6, 1.5F, 6);
electrodes[18].transform.position = Vector3(7, 1.5F, 6);
electrodes[19].transform.position = Vector3(8, 1.5F, 6);
electrodes[20].transform.position = Vector3(9, 1.5F, 6);
electrodes[21].transform.position = Vector3(10, 1.5F, 6);
electrodes[22].transform.position = Vector3(6, 1.5F, 11);
electrodes[23].transform.position = Vector3(7, 1.5F, 11);
electrodes[24].transform.position = Vector3(8, 1.5F, 11);
electrodes[25].transform.position = Vector3(5.5F, 1.5F, 10);
electrodes[26].transform.position = Vector3(7, 1.5F, 10);
electrodes[27].transform.position = Vector3(0, 0, 400);
electrodes[28].transform.position = Vector3(5, 1.5F, 8);
electrodes[29].transform.position = Vector3(6, 1.5F, 8);
electrodes[30].transform.position = Vector3(7, 1.5F, 8);
electrodes[31].transform.position = Vector3(8, 1.5F, 8);
electrodes[32].transform.position = Vector3(9, 1.5F, 8);
electrodes[33].transform.position = Vector3(10, 1.5F, 8);
electrodes[34].transform.position = Vector3(4, 1.5F, 7);
electrodes[35].transform.position = Vector3(5, 1.5F, 7);
electrodes[36].transform.position = Vector3(140, 0, 400);
electrodes[37].transform.position = Vector3(0, 0, 400);
electrodes[38].transform.position = Vector3(-140, 0, 400);
electrodes[39].transform.position = Vector3(9, 1.5F, 7);
electrodes[40].transform.position = Vector3(10, 1.5F, 7);
electrodes[41].transform.position = Vector3(4, 1.5F, 6);
electrodes[42].transform.position = Vector3(5, 1.5F, 6);
electrodes[43].transform.position = Vector3(6, 1.5F, 6);
electrodes[44].transform.position = Vector3(7, 1.5F, 6);
electrodes[45].transform.position = Vector3(8, 1.5F, 6);
electrodes[46].transform.position = Vector3(9, 1.5F, 6);
electrodes[47].transform.position = Vector3(10, 1.5F, 6);
electrodes[48].transform.position = Vector3(6, 1.5F, 11);
electrodes[49].transform.position = Vector3(7, 1.5F, 11);
electrodes[50].transform.position = Vector3(8, 1.5F, 11);
electrodes[51].transform.position = Vector3(5.5F, 1.5F, 10);
electrodes[52].transform.position = Vector3(7, 1.5F, 10);
electrodes[53].transform.position = Vector3(10, 1.5F, 7);
electrodes[54].transform.position = Vector3(4, 1.5F, 6);
electrodes[55].transform.position = Vector3(5, 1.5F, 6);
electrodes[56].transform.position = Vector3(6, 1.5F, 6);
electrodes[57].transform.position = Vector3(7, 1.5F, 6);
electrodes[58].transform.position = Vector3(8, 1.5F, 6);
electrodes[59].transform.position = Vector3(9, 1.5F, 6);
electrodes[60].transform.position = Vector3(10, 1.5F, 6);
electrodes[61].transform.position = Vector3(6, 1.5F, 11);
electrodes[62].transform.position = Vector3(7, 1.5F, 11);
electrodes[63].transform.position = Vector3(8, 1.5F, 11);
electrodes[64].transform.position = Vector3(5.5F, 1.5F, 10);
n=1;
}
function Update () {
// pick a colour from an array and apply to an object
for (var i = 0; i < 64; i++) {
electrodes[i].gameObject.renderer.material.color = (colorArray[i][n]); //as Array)[j];//System.Single.Parse(colorArray[j]);;}
n++;
}
}
Hmm. You created the objects themselves via script. Do they have a material assigned for you to change the colour?
That would work.
var my$$anonymous$$aterial : $$anonymous$$aterial;
function Start()
{
renderer.material = my$$anonymous$$aterial;
}
Aww my +1 disappeared when I converted to answer ><
I do see you are creating primitives, which should make them appear in scene. I can't tell as I can't run your code.
Could you post your colours text file? I can try the script then.
I modified the script to read from one txtfile at the moment, just for testing.
Your Split operation returns an error as it requires a String[] not a String. If you required that to be a character, use ' '
.
I say this as there does not appear to be an overload for (String[], Int32), only (String[], Int32, StringSplitOptions).
Curiously, if I remove that line the Object Reference error on the first Create Primitive line disappears.
Never, ever use the JS Array class. It's slow, obsolete, and makes it hard to code. Also, you desperately need to learn about arrays and loops, since your code is hundreds of times longer than it should be. Plus, never run code outside functions, only declare variables (and use sensible variable names). This:
var col = new Array(64);
should be:
var colors : Color[];
Inside the Start function you would do this:
colors = new Color[64];
This:
var e1 : GameObject;
// etc.
should be:
var electrodes : GameObject[];
Then use a loop:
for (var i = 0; i < electrodes.Length; i++) {
electrodes[i].renderer.material = my$$anonymous$$aterial;
// etc.
And so on.
Yeah we were getting there :P Its why I suggested the $$anonymous$$atlab output be numerical rather than a string
http://docs.unity3d.com/ScriptReference/Color-operator_Vector4.html
Converting between Color and Vector4 is very simple, so as Eric suggested, the Color array would have a huge benefit.
I wanted to let you do it your way, as its a Science project rather than a Game (if I'm not mistaken). Trust Eric to come and drop a bomb ;)
Its essentially the same as what you've got. But ins$$anonymous$$d of Color.green you'd print (0, 1, 0, 1) which is apparently RGBA Green.
You must note that as you are reading the info from a text file even numerical values are actually strings or characters. Its just far easier to tell Unity that '0' is number 0 and '1' is number 1 than trying to tell it that "Color.green" (the string not the alias) is actually (0, 1, 0, 1). The former is a direct parse and works for the full RGBA range. The latter requires a table of conversion and only works for the values you've entered into your table.
I really hope all this crap I'm chatting makes sense.
Realistically, don't worry about loops and simplifying your code, yet. Get it working first or you are adding too many factors in to the potential failure. Once it works and you know what is going on, make it elegant.
Answer by meat5000 · Feb 10, 2015 at 02:02 AM
#pragma strict
import System.IO;
var colorArray : Color[];
var colorStringArray : String[];
var myMaterial : Material;
var e1 : GameObject;
// ~Data Omitted~
var e64 : GameObject;
public function ParseColor (col : String) : Color
{
//Takes strings formatted with numbers and no spaces before or after the commas:
// "1.0,1.0,.35,1.0"
var strings = col.Split(","[0] );
var output : Color;
for (var i = 0; i < 4; i++)
{
output[i] = System.Single.Parse(strings[i]);
}
return output;
}
function Start ()
{
colorArray = new Color[64];
var txtname = "colours2.txt";
var sr : StreamReader = new System.IO.StreamReader(Application.dataPath + "/" + txtname);
while(!sr.EndOfStream)
{
var fromtxt : String;
try
{
fromtxt = sr.ReadLine();
if(fromtxt == null)
{
sr.Close();
}
}
catch (e)
{
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
colorStringArray = fromtxt.Split(";"[0]);
}
if(sr.EndOfStream){ sr.Close(); }
for (var k = 0; k < 64; k++)
{
colorArray[k] = ParseColor(colorStringArray[k]);
}
// Electrodes are placed over the model of the brain in positions indicated in the picture given by EEG laboratory
e1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
e1.transform.position = Vector3(4, 1.5F, 8); //edit positions, F for float
e1.renderer.material = myMaterial;
e1.renderer.material.color = Color.grey;
// ~Data Omitted~
e64 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
e64.transform.position = Vector3(7, 1.5F, 2);
e64.renderer.material = myMaterial;
e64.renderer.material.color = Color.grey;
}
function Update ()
{
e1.gameObject.renderer.material.color = (colorArray[1]);
// ~Data Omitted~
e64.gameObject.renderer.material.color = (colorArray[64]);
}
Left most of your code as it was. Made some small changes. The Parser script was from @Peter-G
Note that the value of Application.dataPath changes depending on the build platform.
Also, the use of Update here is a little needless, unless you plan on changing the values of the colorArray externally. The color doesnt really need to be updated every frame.
The colour has to be updated every frame (look at our conversation) because for every channel every frame another colour in its array should be applied, this is main goal of this script, that's why it exists.
It shows recording of EEG, how electrical activity was changing every milisecond in slow motion.
In this case, you will probably need to adjust the streamreader to be more accurate in taking in data. Its a bit crude at the moment :) Also, the script is set up to read the data from one file only.
The pieces are there, you will need to add the rest back in that I stripped out to debug it.
Follow this Question
Related Questions
How to dynamically create a cube and color it? 2 Answers
Type function(): Object[] does not support Slicing 1 Answer
How to set parents of all objects in an array? 1 Answer
Material rendering black in iOS and Android when assigned in code. 2 Answers
How do I access a different material via Javascript? 1 Answer