刮刮乐小游戏的代码
要注意的一点 遮罩的图片
using Stein.Commons.Bundles;
using Stein.Commons.Network;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(UITexture))]
public class QuestScratchTicket : ASteinUIBase
{
private UITexture mUITex;
UILabel m_DOne;
GameObject m_Fulfill;
private Texture2D PreTex;
private Texture2D MyTex;
public int Radius = 40;
private Color Col = new Color(0, 0, 0, 0);
Color[] s2;
GameObject m_Guidance;
UITexture m_Coent;
float num = 0;
float count = 0.5f;
GameObject mBody;
GameObject mCloseFulltBtn;
byte[][] mArrays;
int mCleanArrayNum = 0;
void ChangePixelColorByCircle (int x, int y, int radius, Color col)
{
int myTexHeight = MyTex.height;
int myTexWidth = MyTex.width;
int radiusSqr = radius * radius;
for (int i = -Radius; i < Radius; i++)
{
var py = y + i;
if (py < 0 || py >= myTexHeight)
{
continue;
}
for (int j = -Radius; j < Radius; j++)
{
var px = x + j;
if (px < 0 || px >= myTexWidth)
{
continue;
}
if (new Vector2(px - x, py - y).sqrMagnitude > radiusSqr)
{
continue;
}
//if (px < uiTexWidth && py < uiTexHeight)
{
if(mArrays[px][py] != 1)
{
mCleanArrayNum++;
mArrays[px][py] = 1;
MyTex.SetPixel(px, py, Col);
}
}
}
}
MyTex.Apply();
if (mCleanArrayNum / (float)(myTexWidth * myTexHeight) >= count)
{
m_DOne.text = ManualLangCommon.GetString(1000722);
mUITex.GetComponent<TweenAlpha>().enabled = true;
m_DOne.text = "";
if (setgg)
{
ReqRequestData(mRoleEffec4, m_Coent.gameObject, "26019");
setgg = false;
}
mBody.SetActive(false);
mCloseFulltBtn.SetActive(true);
Invoke("PlayRight", 4.0f);
}
}
Vector2 WorldPos2Pix (Vector3 worldPos)
{
var temp = transform.InverseTransformPoint(worldPos);
var pos = new Vector2(temp.x + mUITex.width / 2, temp.y + mUITex.height / 2);
float rateX = mUITex.width / (float)MyTex.width;
float rateY = mUITex.height / (float)MyTex.height;
return new Vector2((pos.x / rateX), (pos.y / rateY));
}
float time;
bool setgg=true;
Vector2 posA;
Vector3 worldPos;
Vector2 posB;
Vector3 worldPos1;
void Update ()
{
if (Input.GetMouseButton(0))
{
worldPos1 = worldPos;
worldPos = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
m_Guidance.SetActive(false);
posA = WorldPos2Pix(worldPos);
ChangePixelColorByCircle((int)posA.x, (int)posA.y, Radius,Col);
// ChangePixelColorByCircle(posA[0], posA[1], Radius, Col);
}
if (posA != null && posB != posA && posA != Vector2.zero)
{
if (posB == Vector2.zero)
{
posB = posA;
return;
}
float dist = Vector2.Distance(posA, posB);
int offset = 40;
for (int i = offset; i < dist; i += offset)
{
Vector2 pV = Vector2.Lerp(posA, posB, i / dist);
if (setgg)
{
ChangePixelColorByCircle((int)pV.x, (int)pV.y, Radius, Col);
}
}
posB = posA;
}
time += Time.deltaTime;
if (setgg)
{
if (time >= resp.mDetail.countdown)
{
m_DOne.text = ManualLangCommon.GetString(1000723);
if (setgg)
{
//ReqRequestData(mRoleEffec4, mUITex.gameObject);
setgg = false;
}
QuestGameOverStr str = new QuestGameOverStr();
str.questId = mGameId;
str.titleInfo = ManualLangCommon.GetString(1000723);
str.tipsInfo = ManualLangCommon.GetString(68003);
str.finishState = 0;
Module.OpenUI(XBModuleDefine.DefineQuestSmallGames.UI_GameOver, str);
//StartCoroutine(Delayed());
}
else
{
m_DOne.text = ManualLangCommon.GetString(52109) + (resp.mDetail.countdown - (int)time).ToString();
}
}
}
ParticleEffectProxy mRoleEffec4;
public void ReqRequestData (ParticleEffectProxy effectProxy, GameObject EffectObj,string tx)
{
if (mRoleEffec4 != null)
{
mRoleEffec4.Terminate();
mRoleEffec4 = null;
}
if (effectProxy == null)
{
effectProxy = new ParticleEffectProxy(EffectObj);
effectProxy.SetAttachToPanel(true);
effectProxy.SetScale(1);
effectProxy.Play(tx);
}
}
int mGameId;
CausualGameInfo resp;
public override void OnStart (object param = null)
{
resp = param as CausualGameInfo;
mGameId = resp.mDetail.id;
IAsyncResult<GameObject> result = BundleResources.GetResources().LoadAssetAsync<GameObject>(SteinResType.UI.ToString() + "/SmallGameszc" + resp.mDetail.scratchResId);
result.OnCallback((r) =>
{
if (!string.IsNullOrEmpty(r.Error))
{
Debug.LogWarningFormat("加载UI资源 [{0}] 失败.Error:{1}", "SmallGameszc" + resp.mDetail.scratchResId);
return;
}
if (Module.GetOpennedUi(this.mUIIndex) == null)
{
return;
}
m_Coent.mainTexture = r.Result.GetComponentInChildren<UITexture>(true).mainTexture;
});
PreTex = mUITex.mainTexture as Texture2D;
MyTex = new Texture2D(PreTex.width, PreTex.height, TextureFormat.ARGB32,false);
MyTex.SetPixels(PreTex.GetPixels());
MyTex.Apply();
mUITex.mainTexture = MyTex;
mArrays = new byte[MyTex.width][];
for (int i = 0; i < MyTex.width; i++)
{
mArrays[i] = new byte[MyTex.height];
}
mCleanArrayNum = 0;
if (gameObject.GetComponent<UITexture>().enabled)
{
gameObject.GetComponent<UITexture>().enabled = false;
}
fromOrgan = resp.mDetail.fromOrgan;
organGame = resp.mOrgan;
CommonTools.ShowOrHideUI(false);
}
public override void OnStop (object param = null)
{
mUITex.mainTexture = PreTex;
if (MyTex != null)
DestroyImmediate(MyTex);
MyTex = null;
CommonTools.ShowOrHideUI(true);
}