一:使用相机截图
public
Vector2
popupSize
;
public
int
screenshotHeight
;
public
Menu2DController
menu2DController
;
public
string
postText
;
protected
Texture2D
_screenshot
;
protected
bool
_cleanupScreenshot
=
true
;
1:使用相机正在渲染的屏幕截图
public
static
Texture2D
Screenshot
{
get
{
List
<
Camera
>
cameras
=
new
List
<
Camera
>();
cameras
.
Add
(
GameObject
.
FindGameObjectWithTag
(
"MainCamera"
).
GetComponent
<
Camera
>());
foreach
(
GameObject
go
in
GameObject
.
FindGameObjectsWithTag
(
"GUICamera"
)){
cameras
.
Add
(
go
.
GetComponent
<
Camera
>());
}
cameras
.
Sort
((
x
,
y
) =>
x
.
depth
.
CompareTo
(
y
.
depth
));
int
width
=
Screen
.
width
;
int
height
=
Screen
.
height
;
Texture2D
tex
=
new
Texture2D
(
width
,
height
,
TextureFormat
.
RGB24
,
false
);
RenderTexture
rt
=
new
RenderTexture
(
width
,
height
, 24);
GameObject
watermarkGO
=
GameObject
.
FindWithTag
(
"Watermark"
);
if
(
watermarkGO
==
null
){
Debug
.
LogError
(
"No watermark found!"
);
}
else
{
//watermarkGO.SetActive(true);
}
foreach
(
Camera
cam
in
cameras
){
RenderTexture
current
=
cam
.
targetTexture
;
cam
.
targetTexture
=
rt
;
bool
enabled
=
cam
.
enabled
;
cam
.
enabled
=
true
;
cam
.
Render
();
cam
.
enabled
=
enabled
;
cam
.
targetTexture
=
current
;
}
if
(
watermarkGO
!=
null
){
//watermarkGO.SetActive(false);
}
RenderTexture
.
active
=
rt
;
// Read render texture contents into the texture
tex
.
ReadPixels
(
new
Rect
(0, 0,
width
,
height
), 0, 0 );
tex
.
Apply
();
RenderTexture
.
active
=
null
;
Object
.
Destroy
(
rt
);
return
tex
;
}
}
2:获取已经几区的图片的Texture
protected
IEnumerator
ShowCorutine
()
{
_cleanupScreenshot
=
true
;
_screenshot
=
Screenshot
;
// add some delay, because taking a screenshot
// does a 'hiccup'
yield
return
new
WaitForSeconds
(0.1f);
menu2DController
.
HideButtons
();
Popup
.
Params
p
=
Popup
.
Params
.
Default
;
p
.
header
=
LocalizationManager
.
GetLocalizedText
(
HEADER
);
p
.
dimensions
=
popupSize
;
p
.
image
=
_screenshot
;
p
.
imageDimensions
=
new
Vector2
(
Screen
.
width
/ (
float
)
Screen
.
height
*
screenshotHeight
,
screenshotHeight
);
p
.
onHideEnd
=
HandlePopupHideEnd
;
p
.
button1text
=
LocalizationManager
.
GetLocalizedText
(
SHARE_IT
);
p
.
button1handler
=
OnNativeShareClick
;
p
.
onHideStart
=
menu2DController
.
ShowButtons
;
Popup
.
Instance
.
Show
(
p
);
}
3:将已经截图的texture 图片使用字符串进行传输
public
void
OnNativeShareClick
()
{
//Debug.LogError("XXXXX ShareWebChat");
if
(
_screenshot
!=
null
)
{
byte
[]
val
=
_screenshot
.
EncodeToPNG
();
string
bytesString
=
System
.
Convert
.
ToBase64String
(
val
);
AriesGame
.
Instance
.
WeChatShare
(
bytesString
);
}
else
{
Debug
.
LogError
(
"XXXXX _screenshot = null !!!!!!!!!!!!! "
);
}
//SPShareUtility.ShareMedia ("Kickerinho", GetDescription (), _screenshot);
}
ps:
Popup
.
Params 是一个
弹出框 可以做一个弹出框的预制件
// constructor with default values
public
enum
Layout
{
TEXT_ONLY
,
IMAGE_TEXT
,
IMAGE_STATS
}
// constructor with default values
public
static
Params
Default
{
get
{
Params
p
;
p
.
content
=
""
;
p
.
formatContent
=
true
;
p
.
header
=
""
;
p
.
showCloseButton
=
true
;
p
.
position
=
Vector3
.
zero
;
p
.
dimensions
=
new
Vector2
(400, 200);
p
.
button1text
=
""
;
p
.
button2text
=
""
;
p
.
button1handler
=
null
;
p
.
button2handler
=
null
;
p
.
button1sprite
=
"BtnGreen"
;
p
.
button2sprite
=
"BtnBlue"
;
p
.
image
=
null
;
p
.
imageDimensions
=
new
Vector2
(100, 100);
p
.
onHideEnd
=
null
;
p
.
onHideStart
=
null
;
p
.
layout
=
Layout
.
TEXT_ONLY
;
p
.
layoutSprite
=
""
;
p
.
layoutImageDimensions
=
new
Vector2
(22,22);
p
.
stat1text
=
""
;
p
.
stat2text
=
""
;
p
.
statsSprite
=
""
;
p
.
statsImageDimensions
=
new
Vector2
(22,22);
return
p
;
}
}
}