Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Darth Lordi · Sep 26, 2013 at 01:18 PM · sceneassetfile

Managing Scenes and reusable code

I think I've been too clever for my own good.

I've created a basic sliding cube game and in an attempt to make it reusable I have tried instantiate a lot of the blocks based on a text file with the intention of being able to generate different scenes using the same code but swapping out the config files.

     var fileText = File.ReadAllText("Assets/Config/ObjectEditor.txt");
     line = fileText.Split("\n"[0]);
     var savedObj:GameObject = null;
     var objCount = 0;
     var randCube = new Array ();
     for(var i = 0; i < (line.Length); i++) {
         dataArray = line[i].Split(","[0]);
         var posX = (parseFloat(dataArray[1]));
         var posY = (parseFloat(dataArray[2]));
         var posZ = (parseFloat(dataArray[3]));
         var objRotationX = (parseFloat(dataArray[4]));
         var objRotationY = (parseFloat(dataArray[5]));
         var objRotationZ = (parseFloat(dataArray[6]));
         var objID = ((dataArray[7]));
         if ((dataArray[0] == "Cube")) {
             savedObj = Cube;
         }
 
         if ((dataArray[0] == "Blank")) {
             savedObj = Blank;
         }
 
         // Spawn Object
 //        Debug.Log('pong ' + posX + posY + posZ + objRotationX + objRotationY + objRotationZ);
         var obj = Instantiate(savedObj, Vector3(posX, posY, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));
         objCount++;
         obj.name = (dataArray[0] + "");
          if(savedObj == Cube){
              var pepper = "9x9_ct_" + objID;
              var flib = parseInt(objID)-1;
         var textures : Object[] = UnityEngine.Resources.LoadAll("Textures", Texture2D);
         var texture : Texture2D  = textures[flib];
               obj.GetComponent(CubeScript).CubeID = (parseInt(objID));
              obj.renderer.material.mainTexture = texture;
              Debug.Log(flib);
               }


Now I have started to implement other scenes I noticed that the asset folder is shared. As far as I can tell this means I will need to have to rename the files and edit a new copy of the script for each folder, which seems wasteful and calls for a lot of duplication.

Is there another way I can manage the scenes so I only need to use new copies of config files rather than duplicating all the main code?

Comment
Add comment · Show 3
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 Jamora · Sep 26, 2013 at 02:31 PM 0
Share

I'm afraid I didn't quite understand the question... if your script reads the config file, why would you need to duplicate the code?

I do have a few pointers:

  • You can create the scenes in Unity, then save them, and load them with Application.LoadLevel.. seems a lot easier to me.

  • You can use TextAsset to handle text files ins$$anonymous$$d of the C# built-in libraries, I find this to be simpler at times.

avatar image Darth Lordi · Sep 26, 2013 at 02:36 PM 0
Share

The first line of the above code points to the config file.

var fileText = File.ReadAllText("Assets/Config/ObjectEditor.txt");

I (incorrectly) presumed that the scene file and config files would have their own folder structures so they would relate to each other and there would be no problems having multiple, different, ObjectEditor.txt files as they would be in different locations. As they are in a flat folder structure I can only have one file called ObjectEditor.txt.

avatar image tanoshimi · Sep 26, 2013 at 02:57 PM 0
Share

Well you don't have to have a flat folder structure... just create folders called Scene1, Scene2, Scene3 etc. and place the relevant ObjectEditor.txt file in each. Doesn't that solve it?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jdyeager · Oct 15, 2013 at 09:35 PM

If I understand what you're trying to do, I think you would just need to:

  1. Make the path to the config file a public variable in your script

  2. Place the rest of your code in the Awake method of your script

  3. Attach your script to an object in each of your scenes

  4. Change the config file path in the Inspector on that object in each scene

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

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

Related Questions

Saving and loading scene specific data - (safe for scene renaming) 1 Answer

ungrouping assets 0 Answers

Create JPG to HDD from Image In Assets-Folder 0 Answers

cannot open a unity scene 0 Answers

Why does Unity create folder/file only when I re-import the script that triggered it? 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