Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 06, 2013 at 07:27 PM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MrSplosion · Aug 05, 2011 at 06:13 PM · colormaterialsread.txt

Reading a .txt File

So I made this script that allows you to change a color variable. When you click on a GUI button the variable gets written into a .txt file, and it looks like this. My question is how do I take one of these color variables from the file and apply the color to a game object's material? I know it would be something like:

 renderer.materials[0].color = ???;
 renderer.materials[1].color = ???;
 //and so on...


but I dont understand how to read the file and apply the color.

Can someone help me please? Thanks.

EDIT* This is for a standalone game.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Aug 05, 2011 at 09:28 PM

You just have to parse the text file. It doesn't have a standard-format so you have to parse it yourself.
Usually i don't write a whole script for others but i guess it might be useful for others:

// C# Dictionary<string, Color> ParseColorTable(string aText) { Dictionary<string, Color> result = new Dictionary<string,Color>();

 string[] lines = aText.Split('\n');
 foreach (string L in lines)
 {
     if (L.StartsWith("RGBA("))
     {
         // Cut "RGBA(" and split at ")"
         string[] S = L.Substring(5).Split(')');

         // Remove all spaces and split the 4 color values
         string[] values = S[0].Replace(" ","").Split(',');

         // Parse the 4 strings into floats and create the color value
         Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));

         // Read the colorname and remove leading or trailing spaces
         string colorName = S[1].Trim();

         result.Add(colorName,col);
     }
 }
 return result;

}

This function will return a Dictionary which can be used like this:

// C#
var playerColors = ParseColorTable(myTextFile);
renderer.materials[0].color = playerColors["skinColor"];
renderer.materials[1].color = playerColors["hairColor"];

The function is tested and works as long as the file doesn't contain some corrupted things. I don't do much error-checking. Lines that doesn't start with "RGBA(" are ignored


edit

This is just a quick on-the-fly convertion but should be correct:

function ParseColorTable(aText : String) : Dictionary.<String, Color> { var result = new Dictionary.<String, Color>();

 var lines = aText.Split("\n"[0]);
 for (var L in lines)
 {
     if (L.StartsWith("RGBA("))
     {
         // Cut "RGBA(" and split at ")"
         var S = L.Substring(5).Split(")"[0]);

         // Remove all spaces and split the 4 color values
         var values = S[0].Replace(" ","").Split(","[0]);

         // Parse the 4 strings into floats and create the color value
         var col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));

         // Read the colorname and remove leading or trailing spaces
         var colorName = S[1].Trim();

         result.Add(colorName,col);
     }
 }
 return result;

}

The usage is the same as in C#;)

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sharat · Aug 05, 2011 at 09:44 PM 0
Share

Aw, you beat me to it. :)

avatar image Bunny83 · Aug 05, 2011 at 09:57 PM 0
Share

@sharat: :D sorry, i've even waited 2h before i started to write the function ;)

avatar image MrSplosion · Aug 05, 2011 at 10:23 PM 0
Share

Could someone translate it to java script? I don't understand C# code and especially when I see dictionary's because I've never used them before. And also do I need to import anything else besides System.IO?

avatar image Bunny83 · Aug 05, 2011 at 10:34 PM 0
Share

Well, i'm using the generic Dictionary so you need "System.Collections.Generic" but i'm not sure if that namespace is automatically included in UnityScript or not.

avatar image Bunny83 · Aug 05, 2011 at 10:41 PM 1
Share

I've converted it to UnityScript but haven't tested it yet...

... edit : Tested and works ;)

You have to import the System.Collections.Generic namespace with that line:

 import System.Collections.Generic;
Show more comments
avatar image
1

Answer by Meltdown · Aug 05, 2011 at 06:55 PM

You can do the following using C#

         string line = "";
 
         System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.txt");
         while ((line = file.ReadLine()) != null)
         {
             // Do what you want with the line of text read from the file here...
         }
 
         file.Close();
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrSplosion · Aug 05, 2011 at 08:07 PM 0
Share

Right but do you know how to actually use the line of text read? because once I get the line of text read from the file it prints "RGBA(0.800, 0.533, 0.161, 1.000) skinColor" and that can't be applied to a material's color because of the "skinColor" word. Also the first line is different and so I need a way of just getting the first line and taking out "name" from it.

avatar image
1

Answer by cj_coimbra · Aug 05, 2011 at 07:00 PM

Create a text asset in the game object and then drag and drop your txt file into the text asset slot in that game object.

string fileRead = myTextAsset.text;

Then use the fileRead.Split() function to parse your file. Example

string[] lines = fileRead.Split('\n');

This will fill the array with your file lines. Then, after parsing the lines, you can parse each lines[x] element to do your logic. However you should think of a better set of separators so that the lines can parsed easily.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cj_coimbra · Aug 05, 2011 at 07:01 PM 0
Share

PS: StreamReaders won´t work on OSx or iOS, so you´d better stick with text assets...

avatar image cj_coimbra · Aug 05, 2011 at 07:06 PM 0
Share

Correction: string[] lines = fileRead.Split('\n'); You can throw other chars in the Split function to work as separators and help your parsing. The inverse bar isn´t appearing for some reasing so it is n

avatar image MrSplosion · Aug 05, 2011 at 08:07 PM 0
Share

Right but do you know how to actually use the line of text read? because once I get the line of text read from the file it prints "RGBA(0.800, 0.533, 0.161, 1.000) skinColor" and that can't be applied to a material's color because of the "skinColor" word. Also the first line is different and so I need a way of just getting the first line and taking out "name" from it.

avatar image cj_coimbra · Aug 05, 2011 at 10:20 PM 0
Share

Remove the spaces contained inside the parenthesis then do another Split with (' ') empty char. Then you will separate the RGBA(0.800,0.533,0.161,1.000) from skinColor.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Brightening the colour of a material that can be any colour without shaders. 1 Answer

Does Material.color, ParticleSystem.startcolor property clones/copies the material? 2 Answers

Pressing play Causes Re-instancing of material (Editor) 1 Answer

Instantiated object material colors 1 Answer

One fraction of game object one material, other fraction different material? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges