/// <summary>Shows the given message above the given object.</summary> public void ShowMessage(string message,GameObject aboveObject){ // We'll use WorldUI's for this - no need to mess around with updating etc. // As a worldUI is like a small screen, it needs some pixel space - that's how much space the message HTML has (100px x 100px). WorldUI messageUI=new WorldUI(110,100); // Put it in pixel perfect mode - this is what makes it "stick" to the camera: messageUI.PixelPerfect=true; // Write the message to it: // Important note! If the message originates from players, don't forget that they could potentially write HTML (scripts especially). // textContent is supported too (e.g. messageUI.document.body.textContent) which will write the message "as is". // We're also going to give it a bit of extra style, e.g. a faded white background: messageUI.document.innerHTML="<div style='padding:5px;background:#ffffffaa;text-align:center;'>"+message+"</div>"; // Parent it to and go to the origin of the gameobject: messageUI.ParentToOrigin(aboveObject); // Make the message destroy itself after 5 seconds: messageUI.SetExpiry(5f); }
public override void OnEnable() { // Dump renderer/filter: MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); // True if we've got the visual guide: bool hasGuide = false; if (mr != null && mr.material != null && mr.material.name.StartsWith("worldUIMaterial")) { // Remove it: GameObject.Destroy(mr); hasGuide = true; // Remove filter too: MeshFilter filter = gameObject.GetComponent <MeshFilter>(); if (filter != null) { GameObject.Destroy(filter); } } // First, figure out the 'aspect ratio' of the scale: Vector3 scale = transform.localScale; transform.localScale = Vector3.one * customWidthInMeter; float yAspect = scale.z / scale.x; if (customRatio > 0) { yAspect = customRatio; } // Calc the number of pixels: int height = (int)((float)PixelWidth * yAspect); // Generate a new UI (the name is optional). // The two numbers are the dimensions of our virtual screen: WorldUI = new WorldUI(gameObject.name, PixelWidth, height); // Settings: WorldUI.PixelPerfect = PixelPerfect; WorldUI.AlwaysFaceCamera = AlwaysFaceTheCamera; if (Expiry != 0f) { WorldUI.SetExpiry(Expiry); } // Give it some content using PowerUI.Manager's Navigate method: // (Just so we can use the same Html/ Url fields - it's completely optional) Navigate(WorldUI.document); // Parent it to the GO: WorldUI.ParentToOrigin(transform); if (hasGuide) { // Rotate it 90 degrees about x (to match up with the guide): WorldUI.transform.localRotation = Quaternion.AngleAxis(90f, new Vector3(1f, 0f, 0f)); } // Set the scale such that the width "fits". // The panel will be PixelWidth wide, so we want to divide by that to get to '1'. // Note that the 10 is because a plane is 10 units wide. // We then multiply it by whatever scale (on x) the user originally wanted. // The y scale is accounted for by the computed pixel height (we don't want to distort it). float scaleFactor = (10f * scale.x) / (float)PixelWidth; WorldUI.transform.localScale = new Vector3(scaleFactor, scaleFactor, scale.y); // Optionally accept input: WorldUI.AcceptInput = InputEnabled; }
public override void OnEnable() { // Watch for any file changes: Watch(); // Dump renderer/filter, unless it's flat: MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); // True if we've got the visual guide: bool hasGuide = false; if (!MakeItFlat && mr != null && mr.material != null && mr.material.name.StartsWith("worldUIMaterial")) { // Remove it: GameObject.Destroy(mr); hasGuide = true; // Remove filter too: MeshFilter filter = gameObject.GetComponent <MeshFilter>(); if (filter != null) { GameObject.Destroy(filter); } } // First, figure out the 'aspect ratio' of the scale: Vector3 scale = transform.localScale; float yAspect = scale.z / scale.x; // Calc the number of pixels: int height = (int)((float)PixelWidth * yAspect); if (MakeItFlat) { // Create it as a FlatWorldUI instead: FlatWorldUI fwUI = new FlatWorldUI(PixelWidth, height); WorldUI = fwUI; // Grab the texture and apply it to the material: if (mr != null) { Material mat = new Material(mr.material.shader); mat.mainTexture = fwUI.texture; mr.material = mat; // The flat version is upside down relative to the 3D one: mat.mainTextureScale = new Vector2(-1f, -1f); mat.mainTextureOffset = new Vector2(1f, 1f); } // Give it some content using PowerUI.Manager's Navigate method: // (Just so we can use the same Html/ Url fields - it's completely optional) Navigate(WorldUI.document); if (InputEnabled) { // Create a box collider: BoxCollider bc = gameObject.AddComponent <BoxCollider>(); // Accept input from it: fwUI.AcceptInputFrom(bc); } } else { // Reset local scale: // transform.localScale=Vector3.one; // Generate a new UI (the name is optional). // The two numbers are the dimensions of our virtual screen: WorldUI = new WorldUI(gameObject.name, PixelWidth, height); // Settings: WorldUI.PixelPerfect = PixelPerfect; WorldUI.AlwaysFaceCamera = AlwaysFaceTheCamera; if (Expiry != 0f) { WorldUI.SetExpiry(Expiry); } // Give it some content using PowerUI.Manager's Navigate method: // (Just so we can use the same Html/ Url fields - it's completely optional) Navigate(WorldUI.document); // Parent it to the GO: WorldUI.ParentToOrigin(transform); if (hasGuide) { // Rotate it 90 degrees about x (to match up with the guide): // WorldUI.transform.localRotation=Quaternion.AngleAxis(90f,new Vector3(1f,0f,0f)); } // Set the scale such that the width "fits". // The panel will be PixelWidth wide, so we want to divide by that to get to '1'. // Note that the 10 is because a plane is 10 units wide. // We then multiply it by whatever scale (on x) the user originally wanted. // The y scale is accounted for by the computed pixel height (we don't want to distort it). float scaleFactor = (10f * scale.x) / (float)PixelWidth; WorldUI.transform.localScale = new Vector3(scaleFactor, scaleFactor, scaleFactor); // Optionally accept input: WorldUI.AcceptInput = InputEnabled; } // Input is always inverted for these: WorldUI.InvertResolve = true; }