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
2
Question by aclee · Nov 01, 2014 at 04:12 PM · javascripteditor

editor script makes editor crash

i'm trying to make an editor script that will take a heightmap and turn it in to a cubic terrain but when i run the script the editor crashes.

 #pragma strict
 class CubeLand extends EditorWindow{
     
     var texture:Texture2D;
     var index:int = 0;
     var options:String[] = ["full", "half", "quarter"];
     var obj:GameObject;
     var size:Vector3;
     
     @MenuItem ("Window/CubeLand")
     
     static function ShowWindow(){
         EditorWindow.GetWindow (CubeLand);
     }
     
     function OnGUI () {
         texture = EditorGUI.ObjectField(Rect(3, 3, Screen.width, 100), "HeightMap", texture, Texture, false);
         index = EditorGUI.Popup(Rect(3, 106, Screen.width, 50), "Resolution: ", index, options);
         if(index == 0){
             //if full res
             if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
                 for(var x:int = 0; x < texture.height; x++){
                     for(var z:int = 0; z < texture.width; z++){
                         obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                         obj.transform.position = Vector3(x, 0, z);
                         obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
                     }
                 }
             }
         }else if(index == 1){
             //if half res
             if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
                 for(var x2:int = 0; x2 < texture.height/2; x2++){
                     for(var z2:int = 0; z2 < texture.width/2; z2++){
                         obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                         obj.transform.position = Vector3(x2, 0, z2);
                         obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
                     }
                 }
             }
         }else if(index == 2){
             //if quarter res
             if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
                 for(var x3:int = 0; x3 < texture.height/4; x3++){
                     for(var z3:int = 0; z3 < texture.width/4; z3++){
                         obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                         obj.transform.position = Vector3(x3, 0, z3);
                         obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
                     }
                 }
             }
         }
     }
 }
Comment
Add comment · Show 1
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 robertbu · Nov 01, 2014 at 04:29 PM 0
Share

How big an image are you using? I just ran a quick test with a 32 x 32 image, and it worked...but it took a surprising amount of time for 1$$anonymous$$ of cubes. If you were using a 512 x 512 heightmap, you would be producing a quarter of a million cubes. Unity will not handle that number of game objects well. Note you may want to look into Voxels as an approach.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Nov 01, 2014 at 04:51 PM

Well, "when i run the script" is not very specific. Does that mean when you open the editor window? Or when you click one of your buttons? I guess the latter. Also i don't think that the editor crashes, but simply freezes during the double loop.

Keep in mind if you have a 1024x1024 texture you would create one million gameobjects. That's way too much and could actually take several minutes if not longer. Also you would get a horrible performance. Terrains should always be build out of one or a few meshes and you simply changing the vertices / triangles / quads in that mesh.

Also there's another thing wrong, which however shouldn't cause a crash. You have your height / width reversed. The x component, when you pass it to GetPixel, is in the range of 0 to texture.width-1. You however let x go from 0 to texture.height. If the texture is square that doesn't matter, but if it isn't the function would throw an exception.

Another thing about performance is that you should use GetPixels and work with the returned array. using GetPixel in a loop is significantly slower than using GetPixels once and use the array to read the individual pixels.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Sprint Error script? 2 Answers

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

What is a good unityscript writing program? 1 Answer

Sorting through the same variable attached to several GameObjects from least to greatest, then returning an enum state based on that order 0 Answers


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