• 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 karthees · Nov 09, 2014 at 12:36 PM · gameobjectvector3cloneclones

GameObject size center of the screen

I'm downloading 3d model dynamically using Obj Reader and displaying in the scene but sometimes model showing too small and too big. I can't place in center of the screen for all the model. How to do this?

// duplicate the referenced image target newImageTarget3d = Instantiate (ImageTargetTemplate3Dload.gameObject) as GameObject;

         // enable the new result with the same ImageTargetBehaviour: ???
         ImageTracker tracker = TrackerManager.Instance.GetTracker<ImageTracker> ();
         ImageTargetBehaviour imageTargetBehaviour = (ImageTargetBehaviour)tracker.TargetFinder.EnableTracking 
             (targetSearchResult, newImageTarget3d);            
         
         
         StartCoroutine (seperateJsonLoad3D (mTargetMetadata, targetSearchResult.UniqueTargetId , imageTargetBehaviour ));

Obj Reader Library to get the model from server url:

 IEnumerator seperateJsonLoad3D ( string jsonURL, string targetID, ImageTargetBehaviour itb ){
         
         
         WWW www = new WWW (jsonURL);
         yield return www;
         
         jsonText = www.text;
         
         Debug.Log("3D Url load is "+ jsonURL);
         
         JSONObject json = JSONObject.Parse(jsonURL);
         
         type=json.GetString("type");
         url=json.GetString("link");
         modelShowGUIButton=true;
         
         
         
         if(type.Equals("3d")){
             
              modal = GameObject.Find (targetID);
             
         
             if (!modal) { //fetch model from the URL
                 
                 Debug.Log("3D !modal is "+ url);
                 
                 loadingText = (GUIText)GameObject.Find ("LoadingText").GetComponent(typeof(GUIText));
                 
                 
                 // loadingText = GameObject.Find("LoadingText").GetComponent(GUIText);
                 loadingText.enabled = true;
                 
                 var objData = ObjReader.use.ConvertFileAsync (url , true);
         
                 
                 while (!objData.isDone) {
                     loadingText.text = "Loading... " + (objData.progress*100).ToString("f0") + "%";
                     yield return null;
                 }
                 
 
                 
                 loadingText.enabled = false;
 
                 string modelName = objData.gameObjects[0].name;
                 
                 modal = GameObject.Find (modelName); // find the model reference
                 
                 modal.name = targetID; // change the model name to targetID !!!
                 
                 // store the original model somewhere safe!!
                 modal.transform.position = new Vector3 (modal.transform.position.x, 500f, modal.transform.position.z);
                 
             }
             
              modalClone = Instantiate( modal ) as GameObject; //clone the model 
             
             modalClone.transform.localScale += new Vector3(250,250,250);
 
             /modalClone.AddComponent(MeshCollider);
             mesh = modalClone.AddComponent<MeshCollider>();
 
             modalClone.transform.parent = itb.gameObject.transform; // pair the clone to the instantiated image target
 
         modalClone.transform.localPosition = new Vector3(0,0,0);
 
 
 
 
         }
         
         
         
     }
     

alt text

alt text

3d_destroy.jpg (100.8 kB)
3dship.jpg (98.7 kB)
Comment
Add comment · Show 6
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 09, 2014 at 03:45 PM 0
Share

Is this runtime or edit time? Or is it runtime just because you don't know how to do it at edit time? Is the problem the size or the pivot or both?

avatar image karthees · Nov 09, 2014 at 04:26 PM 0
Share

Please check my edited code and i'm loading the model from server url then i'm using Boomlagoon JSON to get the value.

avatar image karthees · Nov 09, 2014 at 04:55 PM 0
Share

Please help on this

avatar image robertbu · Nov 09, 2014 at 06:45 PM 0
Share

I'm just guessing about your problem, but I'd start by figuring out a scaling factor. You can use Renderer.bounds or $$anonymous$$esh.bounds to calculate a world size for your object. As for the center, if the mesh is authored offset from a central pivot, you can use the bounds.center to calculate an offset.

avatar image karthees · Nov 10, 2014 at 04:15 AM 0
Share

I used below log:

Debug.Log("size is "+ modalClone.renderer.bounds);

I'm getting below size, size is Center: (297.4, 40606.7, -69713.2), Extents: (21502.9, 11646.4, 13594.6) UnityEngine.Debug:Log(Object)

It varying for different object loading from server based on the model size upload from user. SO, how can we fit proper size to display in the screen

Another model i loaded from server and the log is :

size is Center: (0.0, 0.1, 0.2), Extents: (0.5, 0.8, 0.9)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Nov 10, 2014 at 07:06 AM

You don't have enough information here for an exact answer. But assume that you want to scale based on the height of the view. Camera, both orthographic and perspective, given the same settings see the same amount vertically across all devices. So you can do something like this:

    public float goalVertical = 6.5f;  

...then in your code you can do:

    float factor = goalVertical / renderer.bounds.size.y; 
    modalClone.transform.localScale = new Vector3(factor,factor,factor);



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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

GetComponent of ALL clones? 2 Answers

spawning game objects 1 Answer

My script only makes two clones. 3 Answers

Accessing game object based on position 2 Answers

2D Get Object Height to stack up on the screen 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