Skip to content

Commit a619f02

Browse files
committed
Finish the second part-Play with GameObject
Finish the second part-Play with GameObject
1 parent a6e9b49 commit a619f02

File tree

91 files changed

+403
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+403
-3
lines changed

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.5 Play with Linecast.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//PlayWithLinecast
2+
3+
using UnityEngine;
4+
using System.Collections;
5+
6+
public class PlayWithLinecast : MonoBehaviour
7+
{
8+
//--------------------------------变量定义部分---------------------------------
9+
//敌人对象的引用 || Reference to sample enemy object
10+
public GameObject Enemy = null;
11+
//限制线段检测的掩膜层级 || Layer mask to limit line detection
12+
public LayerMask LM;
13+
14+
//-----------------------------------------【Start()函数】---------------------------------------------
15+
// 说明:此函数仅在Update函数第一次被调用前被调用,常用于进行数据的初始化
16+
//--------------------------------------------------------------------------------------------------------
17+
void Start ( )
18+
{
19+
20+
}
21+
22+
//-----------------------------------------【Update()函数】----------------------------------------
23+
// 说明:此函数在每一帧中都会被调用
24+
//------------------------------------------------------------------------------------------------------
25+
void Update ( )
26+
{
27+
//若路径上没有障碍
28+
if(!Physics.Linecast(transform.position,Enemy.transform.position, LM))
29+
{
30+
Debug.Log ("路径上没有障碍~!");
31+
}
32+
//否则,路径上有障碍
33+
else
34+
{
35+
Debug.Log("路径被挡住了~!");
36+
}
37+
}
38+
39+
//-----------------------------------------【OnDrawGizmos()函数】--------------------------------
40+
// 说明:在此函数中书写绘制Gizmo的相关代码
41+
//--------------------------------------------------------------------------------------------------------
42+
void OnDrawGizmos()
43+
{
44+
//设置Gizmo的颜色为蓝色
45+
Gizmos.color = Color.red;
46+
//绘制一条直线
47+
Gizmos.DrawLine(transform.position, Enemy.transform.position);
48+
}
49+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.5 Play with Linecast/PlayWithLinecast.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.5 Play with Linecast/PlayWithLinecast.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.6 Accessing Object Hierarchies.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//AccessingObjectHierarchies
2+
3+
using UnityEngine;
4+
using System.Collections;
5+
6+
public class AccessingObjectHierarchies : MonoBehaviour
7+
{
8+
//--------------------------------变量定义部分---------------------------------
9+
//子对象
10+
private GameObject Child;
11+
//父对象
12+
private GameObject Parent;
13+
14+
//-----------------------------------------【Start()函数】---------------------------------------------
15+
// 说明:此函数仅在Update函数第一次被调用前被调用,常用于进行数据的初始化
16+
//--------------------------------------------------------------------------------------------------------
17+
void Start ( )
18+
{
19+
//获取子对象
20+
Child = GameObject.Find("Child");
21+
Parent = GameObject.Find("Parent");
22+
23+
//设置父对象子对象层级
24+
Child.transform.parent = Parent.transform;
25+
26+
//进行测试
27+
for (int i = 0; i < Parent.transform.childCount; i++)
28+
{
29+
//Print name of child to console
30+
Debug.Log("发现子对象,名为"+Parent.transform.GetChild(i).name);
31+
}
32+
}
33+
34+
//-----------------------------------------【Update()函数】----------------------------------------
35+
// 说明:此函数在每一帧中都会被调用
36+
//------------------------------------------------------------------------------------------------------
37+
void Update ( )
38+
{
39+
40+
}
41+
42+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.6 Accessing Object Hierarchies/AccessingObjectHierarchies.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.6 Accessing Object Hierarchies/AccessingObjectHierarchies.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.8 Use DontDestroyOnLoad.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//PersistentObj
2+
//保持对象在场景跳转时依然存在
3+
4+
using UnityEngine;
5+
using System.Collections;
6+
7+
public class PersistentObj : MonoBehaviour
8+
{
9+
//--------------------------------变量定义部分---------------------------------
10+
11+
12+
//-----------------------------------------【Start()函数】---------------------------------------------
13+
// 说明:此函数仅在Update函数第一次被调用前被调用,常用于进行数据的初始化
14+
//--------------------------------------------------------------------------------------------------------
15+
void Start ()
16+
{
17+
//使当前挂载了此脚本的对象在新场景载入时不被销毁
18+
DontDestroyOnLoad(gameObject);
19+
}
20+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.8 Use DontDestroyOnLoad/PersistentObj.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//SceneChanger
2+
//按下空格便跳转到指定场景
3+
4+
using UnityEngine;
5+
using System.Collections;
6+
7+
public class SceneChanger : MonoBehaviour
8+
{
9+
//--------------------------------变量定义部分---------------------------------
10+
//要跳转到的场景号
11+
public int DestinationScene = 0;
12+
13+
//-----------------------------------------【Update()函数】----------------------------------------
14+
// 说明:此函数在每一帧中都会被调用
15+
//------------------------------------------------------------------------------------------------------
16+
void Update ()
17+
{
18+
//若按下空格,就跳转到目标场景
19+
if(Input.GetKeyDown(KeyCode.Space))
20+
Application.LoadLevel(DestinationScene);
21+
}
22+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.8 Use DontDestroyOnLoad/SceneChanger.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.8 Use DontDestroyOnLoad/scene_01.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.8 Use DontDestroyOnLoad/scene_02.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//GameManager
2+
//游戏管理类-使用单例模式
3+
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
public class GameManager : MonoBehaviour
9+
{
10+
//-----------------------------------------
11+
//--------------------------------变量定义部分---------------------------------
12+
//用C#属性来Get单例模式的实例 || C# Property to get access to singleton instance
13+
//只读-只能读取不能set || Read only - only has get accessor
14+
public static GameManager Instance
15+
{
16+
//返回私有实例的引用 || return reference to private instance
17+
get
18+
{
19+
return instance;
20+
}
21+
}
22+
//instance定义
23+
private static GameManager instance = null;
24+
25+
//最高分
26+
public int HighScore = 0;
27+
28+
//游戏是否处于暂停状态
29+
public bool IsPaused = false;
30+
31+
//是否可以输入
32+
public bool InputAllowed = true;
33+
34+
//-----------------------------------------【Start()函数】---------------------------------------------
35+
// 说明:此函数在Start函数调用前被调用,常用于进行数据的初始化
36+
//--------------------------------------------------------------------------------------------------------
37+
void Awake ()
38+
{
39+
//如果有实例在此场景中存在,销毁此实例
40+
if(instance)
41+
{
42+
DestroyImmediate(gameObject);
43+
return;
44+
}
45+
46+
//使当前实例成为唯一的实例
47+
instance = this;
48+
49+
//使此对象在新场景载入时不被销毁
50+
DontDestroyOnLoad(gameObject);
51+
}
52+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton/GameManager.cs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//SceneChangerV2
2+
//按下空格便跳转到指定场景
3+
4+
using UnityEngine;
5+
using System.Collections;
6+
//Will change scene when we press space
7+
public class SceneChangerV2 : MonoBehaviour
8+
{
9+
//--------------------------------变量定义部分---------------------------------
10+
//要跳转到的场景号
11+
public int DestinationScene = 0;
12+
13+
//-----------------------------------------【Update()函数】----------------------------------------
14+
// 说明:此函数在每一帧中都会被调用
15+
//------------------------------------------------------------------------------------------------------
16+
void Update ()
17+
{
18+
//若按下空格,就跳转到目标场景
19+
if(Input.GetKeyDown(KeyCode.Space))
20+
Application.LoadLevel(DestinationScene);
21+
}
22+
}

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton/SceneChangerV2.cs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton/Scenes.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton/Scenes/scene_01.unity.meta

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Mastering Unity Scripting/Assets/Part 2.Play With GameObject/2.9 GameManagerUseSinglton/Scenes/scene_02.unity.meta

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//ScoreSetter
2+
3+
using UnityEngine;
4+
using System.Collections;
5+
6+
public class ScoreSetter : MonoBehaviour
7+
{
8+
//-----------------------------------------【Start()函数】---------------------------------------------
9+
// 说明:此函数仅在Update函数第一次被调用前被调用,常用于进行数据的初始化
10+
//--------------------------------------------------------------------------------------------------------
11+
void Start ()
12+
{
13+
//设置最高分
14+
GameManager.Instance.HighScore = 100;
15+
}
16+
17+
}

0 commit comments

Comments
 (0)