Its not the distance, it works no matter how far it is from camera. Here’s my code. Line 82 is where 2.6 is. It takes a screenshot on a second camera on layer 8:
using UnityEngine;
using System.Collections;
public class getobjectss : MonoBehaviour
{
public GameObject target;
private RenderTexture rt;
public Camera cam;
void Start()
{
rt = new RenderTexture(Screen.width,Screen.height,24,RenderTextureFormat.ARGB32,RenderTextureReadWrite.Linear);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(thisone());
}
}
public IEnumerator thisone()
{
yield return new WaitForEndOfFrame();
target.layer = 8;
Rect a = GUIRectWithObject(target);
a = new Rect((int)a.position.x-5, (int)a.position.y-5, (int)a.width+10, (int)a.height+10);
if (a.xMin < 0)
{
a.xMin = 0;
}
if (a.xMax > Screen.width)
{
a.xMax = Screen.width;
}
if (a.yMin < 0)
{
a.yMin = 0;
}
if (a.yMax > Screen.height)
{
a.yMax = Screen.height;
}
//Debug.Log(a.xMin + ", "+a.xMax+", "+ a.yMin + ", "+ a.yMax);
//Debug.Log(a.width + ", " + a.height);
//Debug.Log(a.position);
cam.targetTexture = rt;
RenderTexture.active = rt;
cam.Render();
Texture2D screenShot = new Texture2D((int)a.width, (int)a.height, TextureFormat.ARGB32, false, true);
screenShot.ReadPixels(a, 0, 0, false);
//byte[] bytes = screenShot.EncodeToPNG();
//string filename = Application.dataPath + "/_SCap/tredg.png";
//System.IO.File.WriteAllBytes(filename, bytes);
RenderTexture.active = null;
cam.targetTexture = null;
target.layer = 0;
screenShot.filterMode = FilterMode.Point;
screenShot.Apply();
if (Camera.main.orthographic == true)
{
Sprite mySprite = Sprite.Create(screenShot, new Rect(0.0f, 0.0f, screenShot.width, screenShot.height), new Vector2(0f, 1f), 100.0f, 0, SpriteMeshType.FullRect);
GameObject go = new GameObject("test");
SpriteRenderer sr = go.AddComponent<SpriteRenderer>() as SpriteRenderer;
//sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
sr.sprite = mySprite;
go.transform.rotation = Camera.main.transform.rotation;
go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(a.position.x, Screen.height - a.position.y, 5));
}
else
{
Sprite mySprite = Sprite.Create(screenShot, new Rect(0.0f, 0.0f, screenShot.width, screenShot.height), new Vector2(0f, 1f), 100.0f, 0, SpriteMeshType.FullRect);
GameObject go = new GameObject("test");
SpriteRenderer sr = go.AddComponent<SpriteRenderer>() as SpriteRenderer;
//sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
sr.sprite = mySprite;
go.transform.rotation = Camera.main.transform.rotation;
go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(a.position.x, Screen.height - a.position.y, 2.6f));
}
}
public static Rect GUIRectWithObject(GameObject go)
{
Vector3 cen = go.GetComponent<Renderer>().bounds.center;
Vector3 ext = go.GetComponent<Renderer>().bounds.extents;
Vector2[] extentPoints = new Vector2[8]
{
WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z+ext.z))
};
Vector2 min = extentPoints[0];
Vector2 max = extentPoints[0];
foreach (Vector2 v in extentPoints)
{
min = Vector2.Min(min, v);
max = Vector2.Max(max, v);
}
return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
}
public static Vector2 WorldToGUIPoint(Vector3 world)
{
Vector2 screenPoint = Camera.main.WorldToScreenPoint(world);
screenPoint.y = (float)Screen.height - screenPoint.y;
return screenPoint;
}
}