Unity Scripts
Here are a few unity scripts written in C# that are used in my Pluralsight courses.
What does the script do?
This script was written to fix a problem with the AR tools within Unity when multiple VuMarks are within the AR’s camera view. The VuMarkToScene script finds grandchild of the VuMark that matches the VuMark Id and makes only it active.
*Disclaimer* These scripts have worked for my projects and proof of concepts. Feel free to use them at your own risk.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
/*
*******Trigger a scene with Matching Vumark Id***********
Place script on a empty game object that is a child of the VuMark.
Create empty game objects with matching VuMark Instance Ids.
inside of those empty game objects put in what you want to be active when the VuMark Id id tracked.
1. Vumark
a. Empty game object with this script (child of VuMark)
i. Empty game object- named same as VuMark Id (child of empty game object with script)
1. Stuff you want to show up whe the Vumark ID is found (Child of empty game object with VuMark Id name)
i. Empty game object- named same as VuMark Id (child of empty game object with script)
1. Stuff you want to show up whe the Vumark ID is found (Child of empty game object with VuMark Id name)
You can make as many as you want. The script will look down the list
and if it finds a matching VuMarkId and a child of the object the script is on,
it will make that object active, if not all objects stay inactive
If you find any errors please email me
Rich
richfiore01@gmail.com
*/
public class VuMarkIdToScene : MonoBehaviour {
public VuMarkTarget vumark;
private VuMarkManager mVuMarkManager;
void Start ()
{
mVuMarkManager = TrackerManager.Instance.GetStateManager().GetVuMarkManager();
}
void Update ()
{
foreach (var bhvr in mVuMarkManager.GetActiveBehaviours())
{
vumark = bhvr.VuMarkTarget;
var VuId = vumark.InstanceId;
print ("Found ID number " + VuId);
foreach (Transform child in transform)
{
if (child.name == VuId.ToString())
{
child.gameObject.SetActive (true);
}
else
{
child.gameObject.SetActive(false);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class AudioDelayScript : MonoBehaviour {
/*This script was designed to be used with Vuforias User Defined Target with Unity, it mutes your audio at the start of your AR simulation.
It is a work-around (an artist hack) to the audio playback that triggers at the start of the scene when using a simple audio listner and an
audio source on a particle system.
Place this script on your camera and it will mute all audio for the WaitForSeconds time in seconds.
.5f is a half second, 1f is a full second
richfiore01@gmail.com
*/
void Start ()
{
AudioListener.volume = 0f;
StartCoroutine ("waitSeconds");
}
IEnumerator waitSeconds()
{
yield return new WaitForSeconds (.7f);
print ("Audio waited");
AudioListener.volume = 1f;
}
void Update ()
{
}
}