- Home /
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);
}
}
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?
Please check my edited code and i'm loading the model from server url then i'm using Boomlagoon JSON to get the value.
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.
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)
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);