Log in or register to post comments

duplicated object

February 26, 2013 - 6:27am #1

Hi Pros,

How can I make a duplicated object when touch 3D models.

I hv a code that I can drag around and move object. But no idea where and what to add to realize this function. I am stucking on script. Im bad on programming.

duplicated object

March 3, 2013 - 10:09am #25

GUILayout provides methods for sizing controls relative to screen width  - so you don't need to use absolute values - http://docs.unity3d.com/Documentation/ScriptReference/GUILayout.html

To change text attributes, use the GUIStyle class. Here are some examples - http://answers.unity3d.com/questions/17683/custom-font-in-guilabel-but-cant-change-its-color.html

duplicated object

March 3, 2013 - 8:50am #24

Hi Thx so much.

I hv fixed almost all problems.

But I wonder how can make my GUI layout can fit both portrait and landscape on screen?

And how can I change the font/color/size of the text in the code below

 void OnGUI() {
		if(targetsFound){
        if (GUI.Button(new Rect(50,700,200,100), "Clone")) {
            mCloneAsap = true;
        }
		if (GUI.Button(new Rect(200,700,100,50), "Delete"))
		{
			mDestroy = true;
		}
		if (GUI.Button(new Rect(350,700,100,50), "Description")) {
            TapSelect();
        }
		if (GUI.Button(new Rect(500,700,100,50), "Exit")) {
				//Application.Quit();
				Quit=true;
				TapSelect();

				
      
        }
		if (mShowScreen)
        {
            // scale the help window to fit device screen size
            // because of this scaling, hardcoded values can be used
            float deviceDependentScale = Screen.width / 480f;
            Vector2 screenCenter = new Vector2(Screen.width / 2f, Screen.height / 2f);
            GUIUtility.ScaleAroundPivot(new Vector2(deviceDependentScale, deviceDependentScale), screenCenter);

            // draw the instruction dialog
				
            GUI.Window(0, new Rect(screenCenter.x - 230, screenCenter.y - 130, 460, 260),
                DrawWindowContent, "", mGUISkin.window);
				
					

            // reset scale after drawing
            GUIUtility.ScaleAroundPivot(Vector2.one, screenCenter);
        }
		}
    }
	void TapSelect() 
	{
   		
						mGUISkin = Resources.Load("UserInterfaceSkin") as GUISkin;
						
						mShowScreen = true;
						
						//Debug.Log("Enter"+ hit.collider.gameObject.name);
					
 
	}
	private void DrawWindowContent(int id)
    {
        // render the text in the window
		if (mShowScreen){
			if (Quit)
		{
			GUI.Label(new Rect(0, 20, 460, 240),warning, mGUISkin.label);
			if (GUI.Button(new Rect(240, 210, 40, 40), "Exit", mGUISkin.button))
        {
            Application.Quit();
		} else if (GUI.Button(new Rect(10, 210, 40, 40), "Back", mGUISkin.button))
				{
					mShowScreen=false;
				}
		}
       else 
		{
				GUI.Label(new Rect(0, 20, 460, 240),mText, mGUISkin.label);
				if (GUI.Button(new Rect(240, 210, 210, 40), "Back", mGUISkin.button))
				{
					mShowScreen = false;
				}
        }
		} 
		
    }

 

duplicated object

March 3, 2013 - 8:50am #23

Hi Thx so much.

I hv fixed almost all problems.

But I wonder how can make my GUI layout can fit both portrait and landscape on screen?

And how can I change the font/color/size of the text in the code below

 void OnGUI() {
		if(targetsFound){
        if (GUI.Button(new Rect(50,700,200,100), "Clone")) {
            mCloneAsap = true;
        }
		if (GUI.Button(new Rect(200,700,100,50), "Delete"))
		{
			mDestroy = true;
		}
		if (GUI.Button(new Rect(350,700,100,50), "Description")) {
            TapSelect();
        }
		if (GUI.Button(new Rect(500,700,100,50), "Exit")) {
				//Application.Quit();
				Quit=true;
				TapSelect();

				
      
        }
		if (mShowScreen)
        {
            // scale the help window to fit device screen size
            // because of this scaling, hardcoded values can be used
            float deviceDependentScale = Screen.width / 480f;
            Vector2 screenCenter = new Vector2(Screen.width / 2f, Screen.height / 2f);
            GUIUtility.ScaleAroundPivot(new Vector2(deviceDependentScale, deviceDependentScale), screenCenter);

            // draw the instruction dialog
				
            GUI.Window(0, new Rect(screenCenter.x - 230, screenCenter.y - 130, 460, 260),
                DrawWindowContent, "", mGUISkin.window);
				
					

            // reset scale after drawing
            GUIUtility.ScaleAroundPivot(Vector2.one, screenCenter);
        }
		}
    }
	void TapSelect() 
	{
   		
						mGUISkin = Resources.Load("UserInterfaceSkin") as GUISkin;
						
						mShowScreen = true;
						
						//Debug.Log("Enter"+ hit.collider.gameObject.name);
					
 
	}
	private void DrawWindowContent(int id)
    {
        // render the text in the window
		if (mShowScreen){
			if (Quit)
		{
			GUI.Label(new Rect(0, 20, 460, 240),warning, mGUISkin.label);
			if (GUI.Button(new Rect(240, 210, 40, 40), "Exit", mGUISkin.button))
        {
            Application.Quit();
		} else if (GUI.Button(new Rect(10, 210, 40, 40), "Back", mGUISkin.button))
				{
					mShowScreen=false;
				}
		}
       else 
		{
				GUI.Label(new Rect(0, 20, 460, 240),mText, mGUISkin.label);
				if (GUI.Button(new Rect(240, 210, 210, 40), "Back", mGUISkin.button))
				{
					mShowScreen = false;
				}
        }
		} 
		
    }

 

duplicated object

March 3, 2013 - 12:54am #22

Glad to hear that your issue is solved!

For the other two questions (Audio, etc.), you can have a look at the Unity3D website;

for instance, this is a tutorial with all details about playing sounds in Unity:

http://docs.unity3d.com/Documentation/Manual/Sound.html

 

duplicated object

March 2, 2013 - 9:28pm #21

Hi, I hv fixed it. Thx so much.

I hv another question.

Since I hv two audio source (one from a video), I hv modified track in order to play sound while tracker is found( code below). However, it will play the two audio at the same time. because I use: Renderer component in rendererComponents. 

So how can specific the tracker so that they will not play as the same time.

And same for Text component, how can I change different GUI layout when tracker changes. Thx so much!!!!!

  private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }
		
		AudioSource[] audioComponents = this.gameObject.GetComponentsInChildren<AudioSource>();
		foreach (AudioSource component in audioComponents) {
			component.audio.Play();
			
			}

 

duplicated object

March 2, 2013 - 9:28pm #20

Hi, I hv fixed it. Thx so much.

I hv another question.

Since I hv two audio source (one from a video), I hv modified track in order to play sound while tracker is found( code below). However, it will play the two audio at the same time. because I use: Renderer component in rendererComponents. 

So how can specific the tracker so that they will not play as the same time.

And same for Text component, how can I change different GUI layout when tracker changes. Thx so much!!!!!

  private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();

        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
        }
		
		AudioSource[] audioComponents = this.gameObject.GetComponentsInChildren<AudioSource>();
		foreach (AudioSource component in audioComponents) {
			component.audio.Play();
			
			}

 

duplicated object

March 2, 2013 - 8:42pm #19

See my answer here - https://developer.vuforia.com/forum/unity-3-extension-technical-discussion/text-displaying-triggering-tracker

You'll need to modify your OnGUI method to check for the tracking status of the target.

duplicated object

March 2, 2013 - 7:07pm #18

Thx so much!!!!!!!. It works.

And one more question, how can I activate GUI button while tracker is found otherwise disbale buttons.

Cuz I thought all scripts only activated when tracker is found, However, OnGUI() is not affected by trackers, it will show as a GUI Layout every frame.

 

duplicated object

March 2, 2013 - 7:07pm #17

Thx so much!!!!!!!. It works.

And one more question, how can I activate GUI button while tracker is found otherwise disbale buttons.

Cuz I thought all scripts only activated when tracker is found, However, OnGUI() is not affected by trackers, it will show as a GUI Layout every frame.

 

duplicated object

March 2, 2013 - 5:44pm #16

It's correct to use their tags, but the tag needs to be  predefined in the Unity Tag Manager, which is in the Unity Editor - see the instructions in that link.

Also Update() is called for every frame automatically - it's a Unity callback inherited from MonoBehaviour.

duplicated object

March 2, 2013 - 5:03pm #15

Hi, I hv fixed the cloning one just by deleting the clear function. I think it is because the clear function does not work, so it always calls Update().

But clone.tag="Clone" Does not work either.

It says "UnityException: Tag: Clone is not defined!"

I've tried serveral method. I think the problem is I destroy the wrong thing -- empty set.

So how can I collect all cloned gameobject ?

 

 

duplicated object

March 2, 2013 - 5:02pm #14

Hi, I hv fixed the cloning one just by deleting the clear function. I think it is because the clear function does not work, so it always calls Update().

But clone.tag="Clone" Does not work either.

It says "UnityException: Tag: Clone is not defined!"

I've tried serveral method. I think the problem is I destroy the wrong thing -- empty set.

So how can I collect all cloned gameobject ?

 

 

duplicated object

March 2, 2013 - 3:54pm #13

If it's always cloning in Update, that suggests that  mCloneAsap is always true.

Be sure that you've defined the "Clone" tag in the tag manager so that it is recognized. And you may want to define it for the clone ( clone.tag = "Clone" ) rather than the object transform.

see: http://answers.unity3d.com/questions/33597/is-it-possible-to-create-a-tag-programmatically.html

duplicated object

March 2, 2013 - 2:50pm #12
Hi Pros,
Here is my code.
I have 2 problems
1, I don't know why when I run this program, it always cloning. I want to clone once when I click the button
2, Destroy all still not working. I want to detroy(clear) all cloned objects.


void Update () {
        if (mCloneAsap) {
            GameObject gameObjectToClone = this.gameObject;
             
            Debug.Log("Cloning object: ");
            Debug.Log("Name: " + gameObjectToClone.name);
                     
            GameObject clone = Instantiate(gameObjectToClone, gameObjectToClone.transform.position, gameObjectToClone.transform.rotation) as GameObject;
 			
            clone.transform.localScale = new Vector3(1,1,1);
	    clone.transform.tag="Clone";
            
            mCloneAsap = false; 
			
			
        }
		if (mDestroy)
		{
//			Debug.LogWarning("********");
//			GameObject[] others = GameObject.FindGameObjectsWithTag("Clone") as GameObject[];
//			Debug.LogWarning("********"+others);
//				foreach(GameObject other in others)
//			{
//				
//				Debug.Log(other);
//				Destroy(other);
//			}
			foreach (Touch touch in Input.touches) 
		{
        	
            	Ray ray = Camera.main.ScreenPointToRay(touch.position);
            	RaycastHit hit;
            	if (Physics.Raycast (ray, out hit)) 
				{
					if( hit.transform.tag=="Clone"){
						Debug.Log("****************"+hit.transform.tag);
						//Application.LoadLevel(hit.collider.gameObject.name);
						 Destroy(hit.transform.gameObject);
					
					}
				
            	}
        	
    	}
		
		}
		
    }

 

duplicated object

March 2, 2013 - 1:06am #11

 

Hi, have you tried verifying (via Debug.Log) that the code actually enters the loop (i.e. that your "others" array 
 
is not empty ) ?

Also, you can simply use Destroy( other ) instead of Destroy( other.gameObject ).

 

 

duplicated object

March 1, 2013 - 4:58pm #10

Hi, I write another script but still does not work. Anyone plz hv a look...Thx so much

if (mDestroy)
		{
			GameObject[] others = GameObject.FindGameObjectsWithTag("Clone") as GameObject[];
				foreach(GameObject other in others)
			{
				Destroy(other.gameObject);
			}
		
		}
		

 

duplicated object

March 1, 2013 - 4:58pm #9

Hi, I write another script but still does not work. Anyone plz hv a look...Thx so much

if (mDestroy)
		{
			GameObject[] others = GameObject.FindGameObjectsWithTag("Clone") as GameObject[];
				foreach(GameObject other in others)
			{
				Destroy(other.gameObject);
			}
		
		}
		

 

duplicated object

March 1, 2013 - 4:22pm #8

For delect all cloned one, 

I hv this code, but it does not work, anyone knows how to fix?

	GameObject[] clean = GameObject.Find("clone");
			foreach (GameObject c in clean)
			{
				Destroy(GameObject);
			}
		
		}

 

duplicated object

March 1, 2013 - 2:25pm #7

Hi,

One more thing, How can I destroy the cloned one by one?

I know I gonna use destroy, but I don't know where to put.

duplicated object

March 1, 2013 - 1:09pm #6

Hi Thx so much.

And for the GUI. Is there any way to show GUI when tracker is found otherwise disable.

And how to add a button to exit the program?

 

duplicated object

February 28, 2013 - 1:35am #5

Sissi,

what David suggests is the best approach (i.e. in particular note the "public Missile projectile" variable, to which you can assign a 3D model prefab via the inspector...).  

(The only adjustment you need to do in that approach is to actually pick up a different 3D model depending on which object you "picked", i.e. you cannot use the Missile all the time...)

Just for info:

I have also tried the "simple" approach of directly cloning the pickedObject (which is the object that you get in your original picking code), which could perhaps be easier to implement in your specific case;

this is the script:

public class ModelCloner : MonoBehaviour {
	
	private bool mCloneAsap = false;
	
	void Update () {
		if (mCloneAsap) {
			GameObject gameObjectToClone = this.gameObject;
			
			Debug.Log("Cloning object: ");
			Debug.Log("Name: " + gameObjectToClone.name);
					
			GameObject clone = Instantiate(gameObjectToClone, gameObjectToClone.transform.position, gameObjectToClone.transform.rotation) as GameObject;

			clone.transform.localScale = new Vector3(1,1,1);
			
			mCloneAsap = false; 
		}
	}
	
	void OnGUI() {
		if (GUI.Button(new Rect(50,50,200,100), "Clone")) {
			mCloneAsap = true;
		}
	}
}

Some important notes:

  1. For this example, I attached the script to the teapot model in the scene hierarchy
  2. You need to explicitely set the localScale via clone.transform.localScale = new Vector3(1,1,1);
  3. You need to change the World Center Mode to NONE (with the Inspector -> see ARCamera -> QCARBehaviour script -> World Center Mode), otherwise the cloned model will just appear on the original one (and you won't be able to see two distinct models, as they overlapp)
  4. In your case, the gameObjectToClone will actually be your pickedObject (instead of this.gameObject)

I tried that with ImageTargets sample and the teapot model and it did work nicely.

 

duplicated object

February 27, 2013 - 1:07pm #4

Try this method..

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

from the doc..


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Missile projectile;
    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            Missile clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Missile;
            clone.timeoutDestructor = 5;
        }
    }
}

 

duplicated object

February 27, 2013 - 10:00am #3

Hi, Thx so much.

However, It only works for a simple 3D model like cube or cylinder. It does not work for complicated model like fbx imported from maya. So how can I fix this?

I remember someone told me it is gameobj problem. so I hv to create a new gameobj and make it parent to my 3d model. It is right?

Thx again, really appreciate!

duplicated object

February 26, 2013 - 1:21pm #2

Hi, 

you could try instantiating a clone of your picked object when the touch phase ends, i.e. modify your code as follows:

   //Set pickedObject to null after touch ends.
            } else if (touch.phase == TouchPhase.Ended) {
                 // Create a clone of the picked object
                 Instantiate(pickedObject, pickedObject.position, pickedObject.rotation); 
                 pickedObject = null;
            }
  

This is just an example, you might need to adapt depending on your specific needs.

You may also want to check the Unity guide:

http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html

 

Log in or register to post comments