Unity3D: Sprite no appearing - unity3d

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;
}

Related

Unity: draw in game view

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));
}

Move GameObject with Sprite Renderer animation

I have an object in which I have added a sprite Render and an animation to see a sprite sheet in motion, the fact is that I want this object to move from one position to another. But I don't understand why it doesn't work.
The first thing I do with this object is to instantiate it to make a clone.
Then in the update I do the following:
Method void:
spriteRenderer = throwObject.spriteRenderer;
GameObject spriteRendererObject = Instantiate(spriteRenderer);
Update:
if(_animationStarted2){
Vector3 startPos = transform.position;
Vector3 targetPos = _targetPos;
spriteRenderer.transform.position = Vector3.MoveTowards(startPos, Vector3.Lerp(startPos, targetPos, 0.1f),speed);
if (startPos == targetPos)
{
_animationStarted2 = false;
Destroy(gameObject, 0.25f);
}
}

How to Display the Texture after using ScreenCapture.CaptureScreenshotAsTexture();

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
);

how to position a UI Canvas at the same point as it's parent game object

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.

Make rectangle in Unity

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.