Canvas Scaler适配模式Canvas Scaler https://blog.csdn.net/st75033562/article/details/52355239
================================
截图效果:
我们选的Canvas Scale缩放模式为 Scale With Screen Size -> Expand
直接上代码:
public class ShareImage : MonoBehaviour {
[CSharpCallLua]
public delegate void ShareCB();
//分享摄像机
Transform shareCamera = null;
//分享摄像机上面的targetTexture
public RenderTexture showImage = null;
[SerializeField]
private RectTransform BGImage = null;
int width = 1136;
int height = 640;
int posX = 0;
int posY = 0;
void Awake()
{
float multiplyX = Screen.width / 1136f;
float multiplyY = Screen.height / 640f;
float multiply = 1;
posX = 0;
posY = 0;
if ((Screen.width / 1136f) < (Screen.height / 640f))
{
multiply = Screen.width / 1136f;
posX = (int)((1136f / 2 + BGImage.anchoredPosition.x) * multiply);
posY = (int)((640 / 2 + BGImage.anchoredPosition.y) * multiply + Screen.height / 2f - 640f / 2 * multiply);
}
else
{
multiply = Screen.height / 640f;
posX = (int)((1136f / 2 + BGImage.anchoredPosition.x) * multiply + Screen.width / 2f - 1136f / 2 * multiply);
posY = (int)((640 / 2 + BGImage.anchoredPosition.y) * multiply);
}
width = (int)(BGImage.sizeDelta.x * multiply);
height = (int)(BGImage.sizeDelta.y * multiply);
}
///
/// 设置摄像机
///
public void SetCamera()
{
//设置摄像机
GameObject mainCamera = GameObject.Find("FBCamera");
if (mainCamera != null)
{
if (shareCamera)
{
shareCamera.gameObject.SetActive(true);
}
else
{
shareCamera = new GameObject("shareCamera").transform;
shareCamera.parent = mainCamera.transform;
shareCamera.localPosition = Vector3.zero;
shareCamera.localEulerAngles = Vector3.zero;
shareCamera.localScale = Vector3.one;
Camera camera = shareCamera.gameObject.AddComponent
camera.targetTexture = showImage;
camera.depth = -10;
camera.fieldOfView = 38;
camera.cullingMask &= ~(1 << 19);
}
StartCoroutine("HideShareCamera");
}
else
{
Debug.LogError("--分享----没有找到----FBCamera");
}
}
///
/// 调用截图分享功能
///
/// 储存截图路劲
/// 截图名字
/// 分享平台
public void JieTu1(string path, string name, string sharePlatform, ShareCB shareCB)
{
path = Path.Combine(Application.persistentDataPath, path);
StartCoroutine(CoScreenShot(path, name, sharePlatform, shareCB));
}
#region 截图 协诚
//截图 协诚
IEnumerator CoScreenShot(string path, string name, string sharePlatform, ShareCB shareCB)
{
yield return new WaitForEndOfFrame();
path = Path.Combine(Application.persistentDataPath, name + ".jpg");
TextureFormat textureFormat = TextureFormat.RGB24;
Texture2D tex = new Texture2D(width, height, textureFormat, false);
tex.ReadPixels(new Rect(posX, posY, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToJPG();
File.WriteAllBytes(path, bytes);
if (shareCB != null) { shareCB(); }
this.Share(path, sharePlatform);
}
#endregion
#region 隐藏摄像机 协诚
//隐藏摄像机 协诚
IEnumerator HideShareCamera()
{
yield return new WaitForEndOfFrame();
if (shareCamera)
{
shareCamera.gameObject.SetActive(false);
}
}
#endregion
private void OnDestroy()
{
Destroy(shareCamera.gameObject);
}
///
/// 调用sdk分享接口
///
/// 分享图片路径
/// 分享的平台
private void Share(string path,string sharePlatform)
{
SDKMgr.Instance.ShareLinkAndPic(sharePlatform, "", path, "");
}
}
qq:23353677