Copy Paste Row Column from Excel into Unity Input Fields all at once
Hello,
I have about 14 cells in a row in Excel, would like to copy that into 14 Unity input boxes all at once.
I copy the 14 cells as per usual workflow in Excel,
Paste starting the "1st" input box in Unity,
all of the "14 cells" data is pasted into the 1st input box....
Work around please?
I would like to achieve this with multiple rows and columns as well...
Thank you!
Answer by toddisarockstar · May 19, 2017 at 11:58 PM
Unity has absolutely nothing to do with excel! unity is not intended as a word processor so it sopports basic reading of text in ascii or UTF8 format....it's going to read text just like basic windows notepad.
so Your best approach would be exporting your text from excel into a basic txt format. then put that into your unity asset folder as a text asset
https://docs.unity3d.com/Manual/class-TextAsset.html
once the text asset is in your game assets. It could be dragged an dropped into an empty text asset variable declaired in one of your scripts.
alternativly unity has the ability to read text directly from a hard drive if you are making a project for PC.
// need this at top of script
using System;
using System.IO;
using System.Diagnostics;
//write a file to a hard drive like this:
File.WriteAllText("C:/somefolder/somefile.txt","blah blah blah");
//read a file on a hard drive like this:
String txt;
if(File.Exists("C:/somefolder/somefile.txt")){
txt=File.ReadAllText("C:/somefolder/somefile.txt");
print("got this: "+txt);}
from there you would need to manualy program into your unity code what to do with it. or how to display it. unity is not going to understand row or colum information from exell. You would have to manually program code yourself for that sort of thing.
Thank you for the response/answer,
Any pointers on how to designate that "blah blah blah" is for example A1 = (1st) blah, A2 = (2nd) blah and so forth?
perhaps the blahs should be divided somehow, example blah|blah|blah, meaning each "|" is referring to another cell for excel, for input box, word etc?
Thanks again
yes...you can split a string into an array of strings at a seporator charactor or string like this:
public String[] txts;
int i;
int i2;
int i3;
void Start(){
string original = "bubbles|turtle|blah|chickens|green|wonkabar|batman|hippie";
txts = original.Split ("|" [0]);
}
void OnGUI (){
i = 0;
i2 = 0;
i3 = 0;
while (i<txts.Length) {
// display what we read
GUI.Label(new Rect(200*i2,80*i3,300,40),txts[i]);
i2++; if(i2>2){i2=0;i3++;}
i++;
}
}
i am not fimiliar with excell or its output. but if you would like to post an example of what you are trying to load i can show you how to manually split it up.
Alright,
Thanks for all the help you have been providing,
Example:
Excel with 6 cells
Copy paste,
In notepad it appears it has a "tab" spacing between each value copied from cells (the script you write above is based on a blackslash, how to adjust it to be based on this spacing?)
Pasting all these "6" values into 6 Unity UIs at the same time by Ctrl+V
Thank you again, very helpful.