unity里面如何让人物死亡之后显示game overover

主题 : (提问)unity3d中有让GameObject隐藏/显示的 方法吗?
级别: 侠客
可可豆: 714 CB
威望: 714 点
在线时间: 207(时)
发自: Web Page
来源于&&分类
(提问)unity3d中有让GameObject隐藏/显示的 方法吗?&&&
本人要实现两个模型的替换&&想让一个模型先隐藏 然后显示的同时隐藏另一个&&不知道有没有相关的方法本人新手&&谢谢高手指点
级别: 侠客
可可豆: 714 CB
威望: 714 点
在线时间: 207(时)
发自: Web Page
? &&&&&&&&&&
级别: 禁止发言
可可豆: 8213 CB
威望: 8213 点
在线时间: 876(时)
发自: Web Page
用户被禁言,该主题自动屏蔽!
级别: 侠客
可可豆: 2595 CB
威望: 2595 点
在线时间: 796(时)
发自: Web Page
引用 引用第2楼aqingde88于 16:37发表的&&:gameObject.renderer.enabled = 或者    gameObject.active = 使用gameObject.active = false之后,是不是用FindObject()就找不到物体了??
级别: 禁止发言
可可豆: 8213 CB
威望: 8213 点
在线时间: 876(时)
发自: Web Page
用户被禁言,该主题自动屏蔽!
级别: 精灵王
可可豆: 3927 CB
威望: 4087 点
在线时间: 3300(时)
发自: Web Page
gameObject.active = false 后  用find是找不物体的
级别: 天使
发帖: 152450
可可豆: 2255414 CB
威望: 2255414 点
在线时间: 1735(时)
发自: Web Page
那就不要find.直接var gameobject变量..
我的博客:QQ:3122828,微博:@unity3
关注本帖(如果有新回复会站内信通知您)
8*2-5 正确答案:11
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版Unity(21)
从视频最后一课向Done_Main.unity场景修改的过程
第二步:我们继续向下看,DestroyByBoundary.cs脚本看来没什么变化,控制退出边界的要销毁,略过
接下来是DestroyByContact.cs脚本,依然点Find References in scene
在scene中出现了GameController物体,但是上面根本没有我们选择的脚本,这是什么鬼,于是我们选择性忽视了这件事,把上面搜索栏中的内容复制到了工程project
的搜索栏中,于是出现了上面图片中的内容,说明这个脚本被挂在这几个预设体上,看一看,不是岩石就是子弹,这也很正常,都是Enemy
既然他们都出现了,我们就先按照模型把他们都建立起来吧,先从model中拖入scene中,然后建立完成后拖入Prefabs文件夹成为预设体
2.简单来看看DestroyByContact.cs脚本中的内容
&&& public&GameObject&explosion;
&&&&public&GameObject&playerExplosion;
&&&&public&int&scoreValue;
&&&&private&Done_GameController&gameController;
&&&&void&Start&()
&&&&&&&&GameObject&gameControllerObject&=&GameObject.FindGameObjectWithTag&(&GameController&);
&&&&&&&&if&(gameControllerObject&!=&null)
&&&&&&&&&&&&gameController&=&gameControllerObject.GetComponent&&Done_GameController&();
&&&&&&&&if&(gameController&==&null)
&&&&&&&&&&&&Debug.Log&(&Cannot&find&'GameController'&script&);
&&&&&&&&}&& //找到tag为GameController的物体,获取上面的Done_GameController脚本实体,至少判断了下是否存在,保证了程序的健壮
&&&&void&OnTriggerEnter&(Collider&other)
&&&&&&&&if&(other.tag&==&&Boundary&&||&other.tag&==&&Enemy&)&
//由于绑在所有的敌人和子弹预设体上,这句等于忽视了对于场地边界和敌人间的进入碰撞,就是可进入场地且敌人间不相互攻击
&&&&&&&&&&&&return;
&&&&&&&&if&(explosion&!=&null)
&&&&&&&&&&&&Instantiate(explosion,&transform.position,&transform.rotation);
//判断不为空后 调用敌人被消灭后的粒子效果,我有个疑问,敌人的子弹的碰撞后也显示效果?,然后去子弹的预设体看了下,它是个null,好吧,那就是子弹不执行这一条,没有粒子效果
&&&&&&&&if&(other.tag&==&&Player&)
&&&&&&&&&&&&Instantiate(playerExplosion,&other.transform.position,&other.transform.rotation);
&&&&&&&&&&&&gameController.GameOver();&&&&&&&&&&&&&&&&&&&&&&&
//如果撞到了Player,执行Player的
&&&&&&&&gameController.AddScore(scoreValue);
&&&&&&&&Destroy&(other.gameObject);
&&&&&&&&Destroy&(gameObject);
//加分,破坏物体
这里其实我觉得这个碰撞的检测放在Player和他的子弹上比较好,因为一般来说,Player和他的子弹较唯一或种类较少,也符合一般人的逻辑,出现一下打不死的Enemy或其他种类也好区分处理
还有我们运行游戏是发现Player是不死的,然后在if(other.tag == &Player&)设置断点再让Player去撞岩石,断点不会触发,那就是说没成功进入这个函数,我们检查岩石和Player的碰撞体组件,发现Player的碰撞体可能由于自动调整版本的关系Is Trigger上层的Convex选项没有亮,导致Is Trigger也是暗的,我们勾选Convex选项,再运行游戏,可以了
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1013次
排名:千里之外
原创:22篇unity3d中有让GameObject隐藏/显示的 方法吗_百度知道
unity3d中有让GameObject隐藏/显示的 方法吗
我有更好的答案
要不占drawcalls 改要提前引用,即不去渲染 改大小 &#47。.color = new Color (1;&#47.,1方法很多; &#47。,移到视野外,或者干脆去掉MeshFilter。,等我想到新的再来加,遍历 Destroy&#47。 自写shader,结合相机渲染layer和隐藏物体假如你不再用的话 改透明度renderer,推荐;&#47。&#47,0); /&#47,最节省
gameObject,1; &#47.SetActive (false);如果shader支持透明通道的话 遮罩mask来隐藏 关闭单独渲染次物体的相机 改layer,满足条件.enabled =/这法子其实不错,要不你就改不回来了,去掉颜色值渲染或者顶点偏移 暂时想到这么多.。,记得xyz都要改0;多个渲染器的话.material,或者合并一点.localScale = Vector3.zero,删掉渲染的mesh: 改position
使用gameObject.active = false之后,不要find.直接var gameobject变量.。
其他类似问题
为您推荐:
unity3d的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Unity3D手机3D游戏开发:如何实现最高分的存储与显示四使用PlayerPrefs存储数据到本地
关于PlayerPrefs的使用与说明请参照这篇文章:
[Unity3D]手机3D游戏开发:场景切换与数据存储(PlayerPrefs 类的介绍与使用)
为了进一步完善上次做的Demo,首先制作一个首页用来跳转到目标页。
在首页的GameStateObj中添加SetPlayerPrefs脚本:
function Update () {
function Start() {
PlayerPrefs.SetString(&Player Name&, &Bob&);//存入字符串
}再添加一个LoadLevelButton脚本创建跳转按钮和相关操作:
var levelToLoad:S
var graphic = TextureGUI(); //(28,23);
var startPoint = Location();
var GUIColor:Color = Color.
var noGuiStyle : GUIS
function Start() {
graphic.setAnchor();
function Update() {
if (graphic.texture){
startPoint.updateLocation();
function OnGUI() {
GUI.color = GUIC
if (GUI.Button(Rect(graphic.offset.x+startPoint.offset.x,graphic.offset.y+startPoint.offset.y,
graphic.texture.width,graphic.texture.height),graphic.texture,noGuiStyle)) {
PlayerPrefs.Save();
Application.LoadLevel(levelToLoad);
运行效果如下:
在上次的HighScoreDisplay脚本中作如下修改即可(Start方法中读取数据):
var boxStartLocation:Vector2; // offset of main GUI element
var center = Location(); // handy class for calculating the center of the screen
function Start() {
Debug.Log(PlayerPrefs.GetString(&Player Name&));
function Update() {
center.updateLocation(); // every frame, check if the screen has changed and re-calculate the screen center value.
// draw a text string to the screen
var textGUIStyle : GUIS // to control the display of text
var roundsGUIStyle : GUIS // to control the display of text for kills
var score = new Highscore[10]; // array of names for the highscore
score[0].name = &&; // Highscore #1
score[1].name = &Papa&;
score[2].name = &Kyle&;
score[3].name = &Tanya&;
score[4].name = &Delano&;
score[5].name = &Justin&;
score[6].name = &Eddie&;
score[7].name = &Josh&;
score[8].name = &Chris&;
score[9].name = &Steve&;
score[0].rounds = 999;
score[0].kills = 999;
var heightOffset: float = 10; // how much space to place between each highscore level
var roundsOffset: // horizontal offset for rounds column
var killsOffset: // horizontal offset for kills column
var numberHighscores:int = 8; // controls how many loops to run to display
function OnGUI() {
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores to display
GUI.Box(Rect(center.offset.x + boxStartLocation.x, // draw a box with the name
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].name,textGUIStyle); // using textGUIStyle
GUI.Box(Rect(center.offset.x + boxStartLocation.x + roundsOffset, // draw a box with the rounds
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].rounds.ToString(),roundsGUIStyle); // using roundsGUIStyle
GUI.Box(Rect(center.offset.x + boxStartLocation.x + killsOffset, // draw a box with the kills
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].kills.ToString(),roundsGUIStyle); // using roundsGUIStyle
}当然这样只能将数据读取并显示在Console面板上:
在主页的场景创建一个脚本GameState_simple.js来处理相关数据:
import PreviewLabs.PlayerP
var timer:float = 10;
private var origTimer:
var maxTimer:
var roundsWon:int = 1;
var enemyKills:int = 0;
var totalEnemyKills:int = 0;
var transitionDelay:float = 0;
function OnGUI() {
if (GUI.Button(Rect(10,70,50,30),&Click&)) {
// Highscore naming scheme
// key starts : &highscore&
// past that, whatever the setting needs to be
PreviewLabs.PlayerPrefs.SetInt(&highscoreCurrentRounds&, roundsWon);
PreviewLabs.PlayerPrefs.SetInt(&highscoreCurrentKills&, totalEnemyKills);
PreviewLabs.PlayerPrefs.Flush();
function Awake() {
origTimer =
function Start() {
maxTimer =
// if there is a GameID, we want to get it and add one
var gameID:
if (PreviewLabs.PlayerPrefs.HasKey(&highscoreCurrentGameID&)) {
gameID = PreviewLabs.PlayerPrefs.GetInt(&highscoreCurrentGameID&);
PreviewLabs.PlayerPrefs.SetInt(&highscoreCurrentGameID&,++gameID);
PreviewLabs.PlayerPrefs.SetInt(&highscoreCurrentGameID&,1);
function EnemyKilled () {
enemyKills += 1;
enemyKills = Mathf.Clamp(enemyKills,0,999999);
totalEnemyKills += 1;
totalEnemyKills = Mathf.Clamp(totalEnemyKills,0,999999);
function WonRound() {
roundsWon++;
var isGameOver:boolean =
function GameOver() {
if (!isGameOver) {
NotificationCenter.DefaultCenter().PostNotification(this, &GameOver&);
isGameOver =
// wait for death to play, something else?
Application.LoadLevel(&highscore&);
这样在初始化的时候点击按钮便会将数据写入本地。
再在后面的脚本中将原脚本的Start方法:
function Start() {
Debug.Log(PlayerPrefs.GetString(&Player Name&));
}做如下修改即可:
function Start() {
Debug.Log(&The value of key 'highscoreCurrentRounds' is: & + PlayerPrefs.GetInt(&highscoreCurrentRounds&));
Debug.Log(&The value of key 'highscoreCurrentKills' is: & + PlayerPrefs.GetInt(&highscoreCurrentKills&));
此时再运行项目就会发现,欢迎界面对数据进行修改后点击按钮即可存之本地,并且再次运行依旧有效:
接下来就是实现PlayerPrefs和本地数据的同步。
我们添加一个SyncPlayerPrefs函数用来实现同步数据:
function SyncPlayerPrefs() {
// our assumption is that the data in player prefs should over-ride the local data
// need this to see if we have player prefs keys and if we do, copy into our local
// if the key does not exist, we need to populate it with the local data
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores
if (PlayerPrefs.HasKey(&highscore& + i + &name& )) {
//copy Player preferences value to local highscore class array
score[i].name = PlayerPrefs.GetString(&highscore& + i + &name&);
//set that player prefs to local data
PlayerPrefs.SetString(&highscore& + i + &name& , score[i].name);
然后再在Update方法中刷新数据并调用Start函数来打印数据:
function Start() {
&& &Debug.Log(&The value of key 'highscore4name' is: & + PlayerPrefs.GetString(&highscore4name&));
&& &Debug.Log(&The value of key 'highscore8name' is: & + PlayerPrefs.GetString(&highscore8name&));
function Update() {
center.updateLocation(); // every frame, check if the screen has changed and re-calculate the screen center value.
SyncPlayerPrefs();
此时运行项目便会发现本来是脚本中的数据已经可以写入到本地了:
可以使用相同的方法写入Rounds好Kills等数据:
function SyncPlayerPrefs() {
// our assumption is that the data in player prefs should over-ride the local data
// need this to see if we have player prefs keys and if we do, copy into our local
// if the key does not exist, we need to populate it with the local data
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores
if (PlayerPrefs.HasKey(&highscore& + i + &name& )) {
//copy Player preferences value to local highscore class array
score[i].name = PlayerPrefs.GetString(&highscore& + i + &name&);
//set that player prefs to local data
PlayerPrefs.SetString(&highscore& + i + &name& , score[i].name);
if (PlayerPrefs.HasKey(&highscore& + i + &rounds& )) {
//copy Player preferences value to local highscore class array
score[i].rounds = PlayerPrefs.GetInt(&highscore& + i + &rounds&);
//set that player prefs to local data
PlayerPrefs.SetInt(&highscore& + i + &rounds& , score[i].rounds);
if (PlayerPrefs.HasKey(&highscore& + i + &kills& )) {
//copy Player preferences value to local highscore class array
score[i].kills = PlayerPrefs.GetInt(&highscore& + i + &kills&);
//set that player prefs to local data
PlayerPrefs.SetInt(&highscore& + i + &kills& , score[i].kills);
但是这样的简单存储一般来说是难以满足需要的。所以我们需要添加一个函数AddNewHighScore:
function AddNewHighscore() {
var curRounds = PlayerPrefs.GetInt(&highscoreCurrentRounds&);
var curKills = PlayerPrefs.GetInt(&highscoreCurrentKills&);
var newHighscorePosition:
// check if the current score are good / above the highscores in the playersPrefs
// are we assuming the local and playerprefs data is the same? - Yes
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores
if (curRounds & score[i].rounds) {
// new score is higher!
newHighscorePosition =
} else if (curRounds == score[i].rounds && curKills & score[i].kills) {
// new score is equal in rounds and kills are greater
newHighscorePosition =
} // if the rounds are equal and kills are less
} // if the rounds are less
Debug.Log (newHighscorePosition);
//if it is calculate what the # of the new houigschore would be
// add it into our data and save it.
}脚本的目的就是将当前的最高分与排行榜进行比较并且如果高于榜中的任意一个元素立即跳出并添加进去。
当然现在还只能在控制台输出数据。比如原始数据是1,12:
跳转到前端后修改数值:
在跳转回排行榜就会发现数据发生了改变:
完整的脚本HighScoreDisplay.js代码如下:
// this is called after a player dies OR from the main menu
// Display the highscore
// track the follow: Player Name, highscore number of the rounds survived and kills
//Upon entering the High Score menu, test if we have a high score and if we do, ask the
// player to enter a name and add it to the High Score save data.
var boxStartLocation:Vector2; // offset of main GUI element
var center = Location(); // handy class for calculating the center of the screen
function Start() {
Debug.Log(&The value of key 'highscoreCurrentRounds' is: & + PlayerPrefs.GetInt(&highscoreCurrentRounds&));
Debug.Log(&The value of key 'highscoreCurrentKills' is: & + PlayerPrefs.GetInt(&highscoreCurrentKills&));
AddNewHighscore();
function Update() {
center.updateLocation(); // every frame, check if the screen has changed and re-calculate the screen center value.
//SyncPlayerPrefs();
//Start();
// draw a text string to the screen
var textGUIStyle : GUIS // to control the display of text
var roundsGUIStyle : GUIS // to control the display of text for kills
var score = new Highscore[10]; // array of names for the highscore
score[0].name = &&; // Highscore #1
score[1].name = &Papa&;
score[2].name = &Kyle&;
score[3].name = &Tanya&;
score[4].name = &Delano&;
score[5].name = &Justin&;
score[6].name = &Eddie&;
score[7].name = &Josh&;
score[8].name = &Chris&;
score[9].name = &Steve&;
score[0].rounds = 99;
score[0].kills = 999;
score[1].rounds = 1;
score[1].kills = 9;
var heightOffset: float = 10; // how much space to place between each highscore level
var roundsOffset: // horizontal offset for rounds column
var killsOffset: // horizontal offset for kills column
var numberHighscores:int = 8; // controls how many loops to run to display
function OnGUI() {
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores to display
GUI.Box(Rect(center.offset.x + boxStartLocation.x, // draw a box with the name
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].name,textGUIStyle); // using textGUIStyle
GUI.Box(Rect(center.offset.x + boxStartLocation.x + roundsOffset, // draw a box with the rounds
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].rounds.ToString(),roundsGUIStyle); // using roundsGUIStyle
GUI.Box(Rect(center.offset.x + boxStartLocation.x + killsOffset, // draw a box with the kills
center.offset.y + boxStartLocation.y + i * heightOffset,
222,34),score[i].kills.ToString(),roundsGUIStyle); // using roundsGUIStyle
// Highscore naming scheme
// key starts : &highscore&
// past that, whatever the setting needs to be
// a specific high score would look like this:
// highscore4name
function AddNewHighscore() {
var curRounds = PlayerPrefs.GetInt(&highscoreCurrentRounds&);
var curKills = PlayerPrefs.GetInt(&highscoreCurrentKills&);
var newHighscorePosition:
// check if the current score are good / above the highscores in the playersPrefs
// are we assuming the local and playerprefs data is the same? - Yes
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores
if (curRounds & score[i].rounds) {
// new score is higher!
newHighscorePosition =
} else if (curRounds == score[i].rounds && curKills & score[i].kills) {
// new score is equal in rounds and kills are greater
newHighscorePosition =
} // if the rounds are equal and kills are less
} // if the rounds are less
Debug.Log (newHighscorePosition);
//if it is calculate what the # of the new houigschore would be
// add it into our data and save it.
function SyncPlayerPrefs() {
// our assumption is that the data in player prefs should over-ride the local data
// need this to see if we have player prefs keys and if we do, copy into our local
// if the key does not exist, we need to populate it with the local data
for( var i:int = 0 ; i & numberH i++ ) { // for the number of highscores
if (PlayerPrefs.HasKey(&highscore& + i + &name& )) {
//copy Player preferences value to local highscore class array
score[i].name = PlayerPrefs.GetString(&highscore& + i + &name&);
//set that player prefs to local data
PlayerPrefs.SetString(&highscore& + i + &name& , score[i].name);
if (PlayerPrefs.HasKey(&highscore& + i + &rounds& )) {
//copy Player preferences value to local highscore class array
score[i].rounds = PlayerPrefs.GetInt(&highscore& + i + &rounds&);
//set that player prefs to local data
PlayerPrefs.SetInt(&highscore& + i + &rounds& , score[i].rounds);
if (PlayerPrefs.HasKey(&highscore& + i + &kills& )) {
//copy Player preferences value to local highscore class array
score[i].kills = PlayerPrefs.GetInt(&highscore& + i + &kills&);
//set that player prefs to local data
PlayerPrefs.SetInt(&highscore& + i + &kills& , score[i].kills);
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
学习Unity脚本推荐:Unity3D官网索引 在Unity中的数据存储和IOS中字典的存储基本相同,是通过关键字实现数据存储与调用. 下面来介绍一下Unity用来存储数据的PlayerPrefs 类. 使用PlayerPrefs可以在在游戏会话中保持并访问玩家偏好设置. 在Mac OS X上PlayerPrefs存储在-/Library/PlayerPre ...
学习Unity脚本推荐:Unity3D官网索引 上一章已经可以实现最高分的本地存储,接下来的任务就是插入最高分并且实现排行榜其余分数的依次下调. 在代码中的修改十分简单,使用for循环依次调整即可: function AddNewHighscore() { var curRounds = PlayerPrefs.GetInt(&highscoreC ...
学习Unity脚本推荐:Unity3D官网索引 游戏中的数据存储是很常见的问题,比如想在高分榜里显示如下的数据: 下面就是HighScore的制作流程. 首先要搭建好显示的框架,包括背景图片和橙色的边框两个部分, 创建一个空的GameObject用来存放这些GUI,并将其命名为HighscoresOBJ,上面拖拽三个脚本文件. 在这三个脚本文件之前,先创建_ ...
学习Unity脚本推荐:Unity3D官网索引 创建脚本文件TestGUI.js: function OnGUI() { GUI.Box(Rect(0,0,120,30),&This is a title&); } 将其拖拽到HighscoresOBJ上面,运行游戏便会发现窗口中多了一行标签: 这显然不是想要的结果. Unity中的原点和 ...
学习Unity脚本推荐:Unity3D官网索引重力感应在手机游戏的开发中非常常见.Unity3D本身集合了重力感应的相关内容.一个简单的JS脚本示范一下重力感应的使用.CSDNGravity.js://物体的贴图 var round : Texture2D; //物体在屏幕中显示的X Y坐标 var x = 0; var y = 0; //物体屏幕显示的最大 ...
学习Unity脚本推荐:Unity3D官网索引 该案例来源是Digital Tutors的视频教程中的项目源码. 项目的下载的地址在:Unity源码之FPS 手机游戏的开发建议使用UnityRemote,可以直接使用安卓机器运行项目,前提是一定要安装该安卓机的驱动. UnityRemote的下载地址:UnityRemote下载 先来说下项目需求,做完之后大概 ...
学习Unity脚本推荐:Unity3D官网索引 Joystick在手游开发中非常常见,也就是在手机屏幕上的虚拟操纵杆,但是Unity3D自带的Joystick贴图比较原始,所以经常有使用自定义贴图的需求. 下面就来演示一下如何实现自定义JoyStick贴图. 首先导入贴图,注意要把默认的Texture改为GUI要不然尺寸会发生改变: 在Inspector面板 ...
学习Unity脚本推荐:Unity3D官网索引 鼠标手势建议使用一款插件FingerGestures:下载地址. 当然也可以自己写脚本. 一个简单的Demo脚本源码如下 //用于绑定参照物对象 var target : T //缩放系数 var distance = 10.0; //左右滑动移动速度 var xSpeed = 250.0;

我要回帖

更多关于 game over图片 的文章

 

随机推荐