• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

I need help with an array 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges