How do you display the actual ScreenCapture texture onto a public GameObject?
For example:
IEnumerator CaptureScreenShot()
{
yield return new WaitForEndOfFrame();
Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
}
From this snippet, I've captured the screenshot, now I want to display this texture onto a public GameObject at runtime.
Simply apply it to a RawImage component's texture
public RawImage _rawImage;
IEnumerator CaptureScreenShot()
{
yield return new WaitForEndOfFrame();
Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
_rawImage.texture = texture;
}
If you need it as Sprite for applying it to an Image or a Material you can also use Sprite.Create like
Sprite mySprite = Sprite.Create(
texture,
new Rect(0.0f, 0.0f, texture.width, texture.height),
new Vector2(0.5f, 0.5f),
100.0f
);
Related
I made a UI Canvas prefab which has an empty game object with the following script attached to it:
float remainderAngle;
Image rotAngleWedge;
Image remAngleWedge;
public Image rotateWedgePrefab;
float zRotation = 0f;
public void PieGraph (float ringRotateAngle, Transform parentTransform) {
remainderAngle = 360f - ringRotateAngle;
rotAngleWedge = Instantiate (rotateWedgePrefab) as Image;
rotAngleWedge.transform.position = parentTransform.position;
rotAngleWedge.transform.SetParent (transform, false);
rotAngleWedge.color = Color.yellow;
rotAngleWedge.fillAmount = ringRotateAngle / 360f;
zRotation = Quaternion.Euler (parentTransform.eulerAngles).z - 45.0f;
rotAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
zRotation -= rotAngleWedge.fillAmount * 360f;
remAngleWedge = Instantiate (rotateWedgePrefab) as Image;
remAngleWedge.transform.position = parentTransform.position;
remAngleWedge.transform.SetParent (transform, false);
remAngleWedge.color = Color.white;
remAngleWedge.fillAmount = remainderAngle / 360f;
remAngleWedge.transform.rotation = Quaternion.Euler (new Vector3 (0, 0, zRotation));
}
Then I called the PieGraph method in the parent game object script after creating the prefab using. I can't seem to get the UI Canvas to be positioned at the same place as its parent object. Please see images of the inspector settings for the Canvas and its child object below. How can I position them at the same place?
UI Canvas Prefab Inspector Settings
UI Canvas Prefab Child Game Object Inspector Settings
You can try to assign RenderMode to world space.
I stuck on the basics of Unity. I want to make a scene for mobiles in which there are four different coloured rectangles that take 25% of the screen each.
I tried making an GameObject Image, "registering" it as a prefab in Inspector.
Below code is an example of how I tried to make a single red rectangle and position it on the (x,y,z) => (0,0,0) coordinates on my scene.
Several problems are present:
Rectangle did not appear
I don't know how to programatically specify width and height of the rectangle
This is how it looks:
public class SceneScript : MonoBehaviour {
public GameObject prefab;
void Start () {
Vector3 pos = new Vector3(0, 0, 0);
GameObject gameObject = Instantiate(prefab);
Image image = gameObject.GetComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
gameObject.transform.position = new Vector3(0, 0, 0);
}
// Update is called once per frame
void Update () {
}
}
Is there an easier solution, or is this the best practice + could you please provide me some hints what should I do?
You probably don't have a Canvas in your heirarchy. Here's how you can programmatically create your Canvas, your Image and their containing GameObjects:
Vector3 pos = new Vector3(0, 0, 0);
GameObject parentGameObject = new GameObject();
Canvas canvas = parentGameObject.AddComponent<Canvas>();
GameObject imageGameObject = new GameObject();
imageGameObject.transform.SetParent(canvas.transform);
Image image = imageGameObject.AddComponent<Image>();
image.color = new Color(1.0F, 0.0F, 0.0F);
imageGameObject.transform.position = pos;
This will create a full-screen, red rectangle. Play around with the RectTransform settings in the inspector after these are created and you should be able to figure out how to size them properly.
How to create an interactive GUI object in Unity - Editor Window?
I can draw the static quadrangle like code below. But I want the effect like the reference video that starts from 0:22 to 0:30.
public class EditorCanvas {
public static void DrawQuad(int x, int y, int width, int height, Color color) {
Rect rect = new Rect(x, y, width, height);
Texture2D texture = new Texture2D(1, 1);
texture.SetPixel(0, 0, color);
texture.Apply();
GUI.skin.box.normal.background = texture;
GUI.Box(rect, GUIContent.none);
}
}
public class MyWindow : EditorWindow {
void OnGUI() {
EditorCanvas.DrawQuad(100, 75, 50, 50, Color.black);
}
}
You can declare a rect which contains the current position of your box. In this example the position is initialized to 0,0 for a size of 100,100.
Then for each time you move the mouse while clicking (EventType.MouseDrag) you add the mouse movement since the last event (Event.delta) to the box position.
In order to get a smoothly drag & drop you have to tell to unity that you have an event so he can repaint. (Event.use)
Rect _boxPos = new Rect(0, 0, 100, 100);
void OnGUI()
{
if (Event.current.type == EventType.MouseDrag &&
_boxPos.Contains(Event.current.mousePosition))
{
_boxPos.position += Event.current.delta;
}
GUI.Box(_boxPos, "test");
if (Event.current.isMouse)
Event.current.Use();
}
So now you can easily adapt your DrawQuad method.
I am trying to display full screen Webcamtexture on android Portrait mode.
but when i am building and testing it on my device its rotated and videoRotationAngle is 90
I am using RawImage as a texture. I have seen on some post that you have to rotate the transform and get the right angle. But the problem is that if I rotate RawImage UI it will no longer full screen view.
var camImage:UnityEngine.UI.RawImage;
var baseRotation:Quaternion;
var webcamTexture:WebCamTexture;
var rotation:UnityEngine.UI.Text;
function Start () {
webcamTexture = new WebCamTexture(Screen.width,Screen.height);
camImage.texture=webcamTexture;
camImage.material.mainTexture = webcamTexture;
webcamTexture.Play();
rotation.text= webcamTexture.videoRotationAngle.ToString(); // to check the angle
}
I know this is late reply but may help someone facing similar issues.
If you are using RawImage as a Texture, below code should help you achieve the rendering both in Potrait and Landscape mode.
Just ensure that you set "Aspect Mode" in "Aspect Ratio Fitter" of Raw Image to "Width Controls Height" or "Height Controls Width" (whichever is larger based on orientation)
Code Snippet
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System.Collections;
public class DeviceCameraController : MonoBehaviour
{
public RawImage image;
public AspectRatioFitter imageFitter;
//set it to either FRONT or BACK
string myCamera = "BACK";
// Device cameras
WebCamDevice frontCameraDevice;
WebCamDevice backCameraDevice;
WebCamDevice activeCameraDevice;
WebCamTexture frontCameraTexture;
WebCamTexture backCameraTexture;
WebCamTexture activeCameraTexture;
// Image rotation
Vector3 rotationVector = new Vector3(0f, 0f, 0f);
// Image uvRect
Rect defaultRect = new Rect(0f, 0f, 1f, 1f);
Rect fixedRect = new Rect(0f, 1f, 1f, -1f);
// Image Parent's scale
Vector3 defaultScale = new Vector3(1f, 1f, 1f);
Vector3 fixedScale = new Vector3(-1f, 1f, 1f);
void Start()
{
// Check for device cameras
if (WebCamTexture.devices.Length == 0)
{
Debug.Log("No devices cameras found");
return;
}
// Get the device's cameras and create WebCamTextures with them
frontCameraDevice = WebCamTexture.devices.Last();
backCameraDevice = WebCamTexture.devices.First();
frontCameraTexture = new WebCamTexture(frontCameraDevice.name);
backCameraTexture = new WebCamTexture(backCameraDevice.name);
// Set camera filter modes for a smoother looking image
frontCameraTexture.filterMode = FilterMode.Trilinear;
backCameraTexture.filterMode = FilterMode.Trilinear;
// Set the camera to use by default
if (myCamera.Equals("FRONT"))
SetActiveCamera(frontCameraTexture);
else if (myCamera.Equals("BACK"))
SetActiveCamera(backCameraTexture);
else // default back
SetActiveCamera(backCameraTexture);
}
// Set the device camera to use and start it
public void SetActiveCamera(WebCamTexture cameraToUse)
{
if (activeCameraTexture != null)
{
activeCameraTexture.Stop();
}
activeCameraTexture = cameraToUse;
activeCameraDevice = WebCamTexture.devices.FirstOrDefault(device =>
device.name == cameraToUse.deviceName);
image.texture = activeCameraTexture;
image.material.mainTexture = activeCameraTexture;
activeCameraTexture.Play();
}
// Make adjustments to image every frame to be safe, since Unity isn't
// guaranteed to report correct data as soon as device camera is started
void Update()
{
// Skip making adjustment for incorrect camera data
if (activeCameraTexture.width < 100)
{
Debug.Log("Still waiting another frame for correct info...");
return;
}
// Rotate image to show correct orientation
rotationVector.z = -activeCameraTexture.videoRotationAngle;
image.rectTransform.localEulerAngles = rotationVector;
// Set AspectRatioFitter's ratio
float videoRatio =
(float)activeCameraTexture.width / (float)activeCameraTexture.height;
imageFitter.aspectRatio = videoRatio;
// Unflip if vertically flipped
image.uvRect =
activeCameraTexture.videoVerticallyMirrored ? fixedRect : defaultRect;
}
}
Let me know if you face any issue.
Well you have two options: you could either pick portrait or landscape and block screen rotation, or you could automatically rotate your object and stretch it according to the screen boundaries. See Screen.height and Screen.length for more info.
(mCamera.videoRotationAngle + 90) % 360 will rotate the texture properly, then assign a localScale that swaps the height and width of the texture to fix the size.
You need to flip and rotate it. If you something like CameraCaptureKit (https://www.assetstore.unity3d.com/en/#!/content/56673) you will see there is diffirent ways of achieving what you want. In CameraCaptureKit there is a customized RawImage component that essentailly fiddles with the UV coordinates of the RawImahge instead of roatting it. That way you can rotate the image 90 or 270 and flip it without worring if the phone is running in landscape mode or what ever.
Im creating a sprite like this:
Sprite.Create(myTexture,new Rect(0,0,myTexture.width,myTexture.height),new Vector2(1,1),100);
But the sprite is not appearing anywhere. What I'm doing wrong?
Because you also need SpriteRenderer to render your sprite.
void Start () {
Sprite mySprite = Sprite.Create(myTex, new Rect(0, 0, myTex.width, myTex.height), new Vector2(1, 1), 100);
GameObject myObj = new GameObject();
SpriteRenderer spriteRenderer = myObj.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = mySprite;
}