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.
Related
i want to draw guidlines in game view like in cinemacine
screenshot
Ive tried Handles.DrawLine and GUI.DrawTexture but they only shows up in scene view not game view
how do i do it
You can try using line renderers of the Unity UI System.
Here's an example of how to edit a line from script.
LineRenderer lineRenderer;
void Start()
{
lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.positionCount = 2;
lineRenderer.SetPosition(0, new Vector3(0, 0, 0));
lineRenderer.SetPosition(1, new Vector3(10, 10, 10));
}
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
);
I know that there are many questions like this on the net but I haven't found a solution to my problem yet...
I'm trying to make a background image that fills the screen. In my game, there is a canvas and inside this Canvas there is a gameObject which has attached the sprite component.
Many solutions includes this code:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
double variable = Camera.main.orthographicSize * 4.0;
float worldScreenHeight = (float)variable;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
transform.localScale = new Vector2(worldScreenWidth / width, worldScreenHeight / height);
}
But I tried it and it makes the gameobject really small.
Any ideas?
At first, select your Canvas game object. in Canvas Scaler component set UI Scale Mode to Constant Pixel Size.
Note that, when you set UI Scale Mode to Scale With Screen Size, your image changes by screen width or height.
Also, Note that your image should be in the center of its parent. Set the transform position and rotation of your image to this:
Now Change your code to this:
void Awake()
{
SpriteRenderer sr = GetComponent<SpriteRenderer>();
if (sr == null) return;
transform.localScale = new Vector3(1, 1, 1);
float width = sr.sprite.bounds.size.x;
float height = sr.sprite.bounds.size.y;
transform.localScale = new Vector2(Screen.width / width, Screen.height / height);
}
Hope it helps you.
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.
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;
}