What Unity script controls Hiding/Showing of Object?

Hello,

I am using Unity to test out Zapworks and its awesome! One thing I am trying to do is be able to trigger an event before the object gets hidden when it loses the target? What script/code fires this off so I can trigger a fade-out before it disappears?

Thanks,
jrDev

Hey again,

I solved this by copying the code used in the Z.cs file from the static class called PipelineCameraPoseWithOrigin and modified it to this:

	public static bool WillTargetHideBegin(IntPtr o, Matrix4x4 pose, float prevPos) {
		
		bool hideWillBegin = false;
		
		float[] arg_pose = new float[16];
		for (int i = 0; i < 4; i++)
			for (int k = 0; k < 4; k++)
				arg_pose[i * 4 + k] = pose[k, i];
		IntPtr ret = zappar_pipeline_camera_pose_with_origin(o, arg_pose);
		float[] retFloats = new float[16];
		Marshal.Copy(ret, retFloats, 0, 16);
		Matrix4x4 retMatrix = new Matrix4x4();
		for (int i = 0; i < 4; i++)
			for (int k = 0; k < 4; k++)
				retMatrix[k, i] = retFloats[i * 4 + k];
				
		float posX = float.Parse(retMatrix[2].ToString());
            
        //Don't check if the position hasn't changed as to not overdo checks
		if(posX != prevPos){
			
            //If X position is 0 then the object is not on screen; thus the object is going to be hidden
			if(posX == 0){
				
				hideWillBegin = true;
				
			}
			
		}
		
		return hideWillBegin;
		
	}

Then I went ahead and made an edited setup with the ZapparCamera.cs file which replaces the UpdatePose class:

void UpdatePose()
    {
        if (anchorOrigin == null)
        {
            if (cameraAttitudeFromGyro)
                m_cameraPose = Z.PipelineCameraPoseWithAttitude(m_pipeline, useFrontFacingCamera);
            else
                m_cameraPose = Z.PipelineCameraPoseDefault(m_pipeline);
        }
        else
        {
        	
        	//Did we turn on delay usage?
        	if(useDelayHide){
        		
	        	Matrix4x4 anchorPose = anchorOrigin.AnchorPoseCameraRelative();
	        
	        	//Reference the previous position
	        	float posX = float.Parse(m_cameraPose[2].ToString());
	        
		        if(WillTargetHideBegin(m_pipeline, anchorPose, posX)){
		        	
		        	startDelay = true;
		        	
		        	//Trigger the new Before Marker Hidden event
		        	anchorOrigin.gameObject.GetComponent<ZapparImageTrackingTarget>().m_OnWillNotBeSeenEvent.Invoke();
	
		        }
		        
		        //Count till reach time before finally hiding the object
		        if(startDelay){
		        	
			        delayTimeCount += Time.deltaTime;
		        
			        if(delayTimeCount > delayTime) {
			        
				        m_cameraPose = Z.PipelineCameraPoseWithOrigin(m_pipeline, anchorPose);
		        		   
				        delayTimeCount = 0f;
					    
	        			startDelay = false;
	        			
		        	}
		        	    
		        }else{
		        	
			        m_cameraPose = Z.PipelineCameraPoseWithOrigin(m_pipeline, anchorPose);
			        
		        }
		        
	        }else{//Otherwise hide objects the default way with no delay
	        	
		        Matrix4x4 anchorPose = anchorOrigin.AnchorPoseCameraRelative();
	        
	        	m_cameraPose = Z.PipelineCameraPoseWithOrigin(m_pipeline, anchorPose);
	        	
	        }
	        
        }

	    Matrix4x4 cameraPoseUnity = Z.ConvertToUnityPose(m_cameraPose);
	    transform.localPosition = Z.GetPosition(cameraPoseUnity);
	    transform.localRotation = Z.GetRotation(cameraPoseUnity);
	    transform.localScale = Z.GetScale(cameraPoseUnity);

    }

Now this should allow you to do whatever you want before the object gets hidden using the new event. Make sure to add that event to the ZapperImageTrackingTarget script:

public UnityEvent m_OnWillNotBeSeenEvent;

Thanks,
jrDev

2 Likes

Hi @jrDev - great job!

We love seeing the different ways that users play around with the Universal AR SDK to make it suit their projects/make it their own - it’s super inspiring!

Have a great day,
Francesca :blush:

1 Like

Yeah!

Had to comment out and test a lot of code to figure out what script controlled the hiding/showing of the object though (which is what I wanted to know) before I could modify the script. Hopefully someone else can find use in this.

Thanks,
rDev

2 Likes