Unity 分享 和 不同分辨率适配

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

 

 

}

 

/// <summary>

/// 设置摄像机

/// </summary>

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

camera.targetTexture = showImage;

camera.depth = -10;

camera.fieldOfView = 38;

camera.cullingMask &= ~(1 << 19);

}

StartCoroutine("HideShareCamera");

}

else

{

Debug.LogError("--分享----没有找到----FBCamera");

}

}

 

/// <summary>

/// 调用截图分享功能

/// </summary>

/// <param name="path">储存截图路劲</param>

/// <param name="name">截图名字</param>

/// <param name="sharePlatform">分享平台</param>

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

}

 

/// <summary>

/// 调用sdk分享接口

/// </summary>

/// <param name="path">分享图片路径</param>

/// <param name="sharePlatform">分享的平台</param>

private void Share(string path,string sharePlatform)

{

SDKMgr.Instance.ShareLinkAndPic(sharePlatform, "", path, "");

}

}

qq:23353677

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值