我正在构建一个由8个级别组成的游戏。我使用以下脚本切换下一级。但是当我解锁一个关卡时,锁纹理仍然保留在窗口中。当我解锁一个新关卡时,我想删除锁定纹理。请帮帮我。
完整的脚本如下所示:
var TitleText:String = "SINGLE PLAYER";
var NumberOfPlayers:int = 1;
var Levels:int = 8;
private var LevelIndex:int = 0;
var PointsToUnlock:Array = new Array(0, 2, 6, 12, 24, 36, 48, 56);
var LevelsGrid:Vector2 = Vector2(4,2);
private var GridIndexA:int = 0;
private var GridIndexB:int = 0;
var LevelTexture:Texture2D;
var LockTexture:Texture2D;
//~ var HowToPlayTexture:Texture2D;
var BGPattern:Texture2D;
//This script displays the HUD, with current weapon, ammo left, Health, Shield, and score
var GUIskin:GUISkin; //The skin gui we'll use
private var LevelPoints:Array = new Array();
private var TotalPoints:int = 0;
function Start()
{
if ( camera.main.GetComponent("StartMenu") )
camera.main.GetComponent("StartMenu").MenuLevel = transform;
else if ( camera.main.GetComponent("EndMenu") )
camera.main.GetComponent("EndMenu").MenuLevel = transform;
LevelPoints.Clear();
for ( LevelIndex = 1 ; LevelIndex <= transform.GetComponent("LevelsMenu").Levels ; LevelIndex++ )
{
LevelPoints.Push(PlayerPrefs.GetInt(("level" + LevelIndex + "Points").ToString()));
TotalPoints += LevelPoints[LevelIndex - 1];
}
print(LevelPoints);
print(TotalPoints);
}
function OnGUI()
{
GUI.skin = GUIskin; //The skin gui we'll use
GUI.depth = 0;
GUI.DrawTexture(Rect( 0, 0 ,Screen.width, Screen.height), BGPattern);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
GUI.Label(Rect(0, 10, Screen.width, 70), TitleText);
LevelIndex = 0;
for ( GridIndexB = 0 ; GridIndexB < LevelsGrid.y ; GridIndexB++ )
{
for ( GridIndexA = 0 ; GridIndexA < LevelsGrid.x ; GridIndexA++ )
{
LevelIndex++;
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10) ,LevelTexture.width, LevelTexture.height), LevelTexture);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
if ( TotalPoints >= PointsToUnlock[LevelIndex - 1] )
{
if ( GUI.Button(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LevelIndex.ToString()) )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
PlayerPrefs.SetInt("currentLevel", LevelIndex);
Application.LoadLevel("level" + LevelIndex);
}
}
else
{
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LockTexture);
}
}
}
if ( GUI.Button(Rect( 10, Screen.height - 70, 140, 60), "BACK") )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
if ( camera.main.GetComponent("StartMenu") )
camera.main.GetComponent("StartMenu").MenuLevel = camera.main.transform;
else if ( camera.main.GetComponent("EndMenu") )
camera.main.GetComponent("EndMenu").MenuLevel = camera.main.transform;
Destroy(gameObject);
}
if ( GUI.Button(Rect( Screen.width - 410 , Screen.height - 70, 400, 60), "RESET LEVELS") )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
LevelPoints.Clear();
for ( LevelIndex = 1 ; LevelIndex <= transform.GetComponent("LevelsMenu").Levels ; LevelIndex++ )
{
PlayerPrefs.SetInt(("level" + LevelIndex + "Points").ToString(), 0);
}
//~ TotalPoints = 0;
}
}
我只需要在以下循环中使用一行简单的代码,我可以在新级别出现时禁用LockTexture:
LevelIndex = 0;
for ( GridIndexB = 0 ; GridIndexB < LevelsGrid.y ; GridIndexB++ )
{
for ( GridIndexA = 0 ; GridIndexA < LevelsGrid.x ; GridIndexA++ )
{
LevelIndex++;
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10) ,LevelTexture.width, LevelTexture.height), LevelTexture);//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
if ( TotalPoints >= PointsToUnlock[LevelIndex - 1] )
{
if ( GUI.Button(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LevelIndex.ToString()) )//Laps.ToString() + "/" + GameController.GetComponent("GameController").LapsToWin.ToString()); //Draw the HUD texture
{
PlayerPrefs.SetInt("currentLevel", LevelIndex);
Application.LoadLevel("level" + LevelIndex);
}
}
else
{
GUI.DrawTexture(Rect( (Screen.width - LevelsGrid.x * (LevelTexture.width + 10)) * 0.5 + GridIndexA * (LevelTexture.width + 10), 60 + GridIndexB * (LevelTexture.height + 10), LevelTexture.width, LevelTexture.height), LockTexture);
}
}
}