کد بهتر یونیتی برای لود کردن سین ها بدون تیک زدن بازی:(از یونیتی 5 به بالا)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; usingUnityEngine.SceneManagement;
public class welcome : MonoBehaviour {
public Button start;
void Start () {
start.onClick.AddListener (() => {
SceneManager.LoadScene("level1");
});
}
void Update () {
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; usingUnityEngine.SceneManagement;
public class welcome : MonoBehaviour {
public Button start;
void Start () {
start.onClick.AddListener (() => {
SceneManager.LoadScene("level1");
});
}
void Update () {
}
}
اجرای فیلم در یونیتی
using UnityEngine;
using System.Collections;
public class movie : MonoBehaviour {
public MovieTexture movTexture;
void Start() {
renderer.material.mainTexture = movTexture;
movTexture.Play();
}
}
using UnityEngine;
using System.Collections;
public class movie : MonoBehaviour {
public MovieTexture movTexture;
void Start() {
renderer.material.mainTexture = movTexture;
movTexture.Play();
}
}
کد لودینگ درصدی در یونیتی
using UnityEngine;
using System.Collections;
public class loading : MonoBehaviour {
public Texture loadTexture;
AsyncOperation async;
void OnGUI()
{
if(GUI.Button(new Rect(20,70,80,20), "Level 1"))
{
load_level();
}
if(async !=null)
{
GUI.DrawTexture(new Rect(,100,async.progress*Screen.width,30),loadTex ture,ScaleMode.ScaleAndCrop);
}
}
void load_level()
{
StartCoroutine(_loading("level"));
}
IEnumerator _loading(string level_name)
{
async = Application.LoadLevelAsync(level_name);
while(!async.isDone)
{
yield return async;
}
}
}
using UnityEngine;
using System.Collections;
public class loading : MonoBehaviour {
public Texture loadTexture;
AsyncOperation async;
void OnGUI()
{
if(GUI.Button(new Rect(20,70,80,20), "Level 1"))
{
load_level();
}
if(async !=null)
{
GUI.DrawTexture(new Rect(,100,async.progress*Screen.width,30),loadTex ture,ScaleMode.ScaleAndCrop);
}
}
void load_level()
{
StartCoroutine(_loading("level"));
}
IEnumerator _loading(string level_name)
{
async = Application.LoadLevelAsync(level_name);
while(!async.isDone)
{
yield return async;
}
}
}
ذخیره مختصات یک باتن در یونیتی_تغییر مختصات button _دستری به position یک button در یونیتی:
public Button button1;
button1.rectTransform.anchoredPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
در کد فوق متغیر button1 از نوع عمومی هست که در inspector ظاهر میشه(پس از اختصاص اسکریپت به گیم ابجکت دلخواه،مثلا یک گیم ابجکت تهی به نام gameController)
برای ذخیره حتی پس از خروج بازی :
PlayerPrefs.SetFloat("x",button1.rectTransform.anchoredPosition.x);
PlayerPrefs.SetFloat("y",button1.rectTransform.anchoredPosition.y);
PlayerPrefs.Save();
برای بازیابی مقادیر x,y و تنظیم مجدد هنگام ورد به بازی، در تابع Start() از کدی مثل استفاده می کنیم:
float x=PlayerPrefs.GetFloat("x");
float y=PlayerPrefs.GetFloat("y");
button1.rectTransform.anchoredPosition = new Vector2(x, y);
اگه new کار نداد به این سبک تست کنید:
button1.rectTransform.anchoredPosition.Set(x, y);
public Button button1;
button1.rectTransform.anchoredPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
در کد فوق متغیر button1 از نوع عمومی هست که در inspector ظاهر میشه(پس از اختصاص اسکریپت به گیم ابجکت دلخواه،مثلا یک گیم ابجکت تهی به نام gameController)
برای ذخیره حتی پس از خروج بازی :
PlayerPrefs.SetFloat("x",button1.rectTransform.anchoredPosition.x);
PlayerPrefs.SetFloat("y",button1.rectTransform.anchoredPosition.y);
PlayerPrefs.Save();
برای بازیابی مقادیر x,y و تنظیم مجدد هنگام ورد به بازی، در تابع Start() از کدی مثل استفاده می کنیم:
float x=PlayerPrefs.GetFloat("x");
float y=PlayerPrefs.GetFloat("y");
button1.rectTransform.anchoredPosition = new Vector2(x, y);
اگه new کار نداد به این سبک تست کنید:
button1.rectTransform.anchoredPosition.Set(x, y);
کد زوم کردن تفنگ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
[Range(0,100)]
public int zoom = 20;
[Range(0,100)]
public int normal = 60;
[Range(0,100)]
public int smooth = 5;
private bool iszoomed = false;
void Update (){
if (Input.GetKeyDown (KeyCode.Mouse0)) {
iszoomed = !iszoomed;
}
if (iszoomed) {
GetComponent<Camera> ().fieldOfView = Mathf.Lerp (GetComponent<Camera> ().fieldOfView, zoom, Time.deltaTime * smooth);
}
else {
GetComponent<Camera> ().fieldOfView = Mathf.Lerp (GetComponent<Camera> ().fieldOfView, normal, Time.deltaTime * smooth);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
[Range(0,100)]
public int zoom = 20;
[Range(0,100)]
public int normal = 60;
[Range(0,100)]
public int smooth = 5;
private bool iszoomed = false;
void Update (){
if (Input.GetKeyDown (KeyCode.Mouse0)) {
iszoomed = !iszoomed;
}
if (iszoomed) {
GetComponent<Camera> ().fieldOfView = Mathf.Lerp (GetComponent<Camera> ().fieldOfView, zoom, Time.deltaTime * smooth);
}
else {
GetComponent<Camera> ().fieldOfView = Mathf.Lerp (GetComponent<Camera> ().fieldOfView, normal, Time.deltaTime * smooth);
}
}
}
آموزش ساخت ساعت در یونیتی .
ابتدا یک گیم آبجکت به نام clock میسازیم .
سپس یه گیم آبجکت hour , minute , second ساخته و زیر مجموعه clock میکنیم .
سه کیوب هم میسازیم و زیر مجموعه سه گیم آبجکت میکنیم.
خوب الان یه کد سی شارپ باز میکنیم :
using UnityEngine;
using System;
public class ClockAnimator : MonoBehaviour { private const float hoursToDegrees = 360f / 12f, minutesToDegrees = 360f / 60f,
secondsToDegrees = 360f / 60f;
public Transform hours, minutes, seconds;
public bool analog;
private void Update () { if (analog) { TimeSpan timespan = DateTime.Now.TimeOfDay;
hours.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalHours * -hoursToDegrees); minutes.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalMinutes * -minutesToDegrees); seconds.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalSeconds * -secondsToDegrees);
} else { DateTime time = DateTime.Now;
hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);
minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);
seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);
}
}
}
بعد هم گیم آبجکت ها رو نسبت دهی کنید .
@Unity_Scripts
ابتدا یک گیم آبجکت به نام clock میسازیم .
سپس یه گیم آبجکت hour , minute , second ساخته و زیر مجموعه clock میکنیم .
سه کیوب هم میسازیم و زیر مجموعه سه گیم آبجکت میکنیم.
خوب الان یه کد سی شارپ باز میکنیم :
using UnityEngine;
using System;
public class ClockAnimator : MonoBehaviour { private const float hoursToDegrees = 360f / 12f, minutesToDegrees = 360f / 60f,
secondsToDegrees = 360f / 60f;
public Transform hours, minutes, seconds;
public bool analog;
private void Update () { if (analog) { TimeSpan timespan = DateTime.Now.TimeOfDay;
hours.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalHours * -hoursToDegrees); minutes.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalMinutes * -minutesToDegrees); seconds.localRotation = Quaternion.Euler( 0f, 0f, (float)timespan.TotalSeconds * -secondsToDegrees);
} else { DateTime time = DateTime.Now;
hours.localRotation = Quaternion.Euler(0f, 0f, time.Hour * -hoursToDegrees);
minutes.localRotation = Quaternion.Euler(0f, 0f, time.Minute * -minutesToDegrees);
seconds.localRotation = Quaternion.Euler(0f, 0f, time.Second * -secondsToDegrees);
}
}
}
بعد هم گیم آبجکت ها رو نسبت دهی کنید .
@Unity_Scripts
public IEnumerator Rot(Vector3 pos)
{
for (int i = 0; i < 100; i++) {
var lookPos = pos - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*2);
print (transform.rotation);
yield return new WaitForSeconds(.05f);
}
}
اسکریپت چرخش آرام به سمت هدف
@Unity_Scripts
{
for (int i = 0; i < 100; i++) {
var lookPos = pos - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*2);
print (transform.rotation);
yield return new WaitForSeconds(.05f);
}
}
اسکریپت چرخش آرام به سمت هدف
@Unity_Scripts
ایجاد اثر تیر روی دیوار/ایچاد اثر گلوله روی دیوار/باقی ماندن اثر گلوله روی دیوار/ماندن جای تیر روی محل برخورد.
برای اینکار پریفب به شکل سوراخ شدن توسط گلوله رو روی نقطه ی برخورد گلوله با دیوار ایجاد می کنیم:
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
مثال دیگر:
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
Instantiate( sourceObject, hit.point, Quaternion.identity );
}
@Unity_Scripts
برای اینکار پریفب به شکل سوراخ شدن توسط گلوله رو روی نقطه ی برخورد گلوله با دیوار ایجاد می کنیم:
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
مثال دیگر:
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
Instantiate( sourceObject, hit.point, Quaternion.identity );
}
@Unity_Scripts
Unity Scripts via @vote
سلام نظرتون در مورد این که یه پکیج آموزشی بسازیم که پولی باشه ولی بعد از دیدن آن واقع بتوانید یک بازی ساز حرفه ای بشود که اگر حتی بخش مبتدی آن را هم ببینید بتوانید یک بازی موفق در سبک های مختلف بسازید
anonymous poll
مخالفم – 69
👍👍👍👍👍👍👍 60%
موافقم – 46
👍👍👍👍👍 40%
👥 115 people voted so far.
anonymous poll
مخالفم – 69
👍👍👍👍👍👍👍 60%
موافقم – 46
👍👍👍👍👍 40%
👥 115 people voted so far.
Media is too big
VIEW IN TELEGRAM
مقدمه پکیج آموزشی فیلم رو ببینید و بعد تو نظر سنجی پایین شرکت کنید
Unity Scripts via @vote
نظر شما حالا در رابطه با پکیج بالا☝ چیه ؟
anonymous poll
نمی خرمش – 47
👍👍👍👍👍👍👍 73%
خوبه می خرمش – 17
👍👍👍 27%
👥 64 people voted so far.
anonymous poll
نمی خرمش – 47
👍👍👍👍👍👍👍 73%
خوبه می خرمش – 17
👍👍👍 27%
👥 64 people voted so far.
🔸متد Sleep() در ترد نویسی
در مبحث ترد نویسی،از این متد برای ایجاد تاخیر در حلقه استفاده می شود.(مثال: 200 میلی ثانیه):
using System;
using System.Threading;
public class MyThread
{
public void Thread1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(200);
}
}
}
در مبحث ترد نویسی،از این متد برای ایجاد تاخیر در حلقه استفاده می شود.(مثال: 200 میلی ثانیه):
using System;
using System.Threading;
public class MyThread
{
public void Thread1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(200);
}
}
}
🔸ایجاد یک تایمر با سیشارپ،ساخت تایمر در سیشارپ
using System;
using System.Timers;
class myApp
{
public static void Main()
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
myTimer.Interval = 1000;
myTimer.Start();
while ( Console.Read() != 'q' )
{
; // do nothing...
}
}
public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
{
Console.Write("\r{0}", DateTime.Now);
}
}
@Unity_Scripts
using System;
using System.Timers;
class myApp
{
public static void Main()
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
myTimer.Interval = 1000;
myTimer.Start();
while ( Console.Read() != 'q' )
{
; // do nothing...
}
}
public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
{
Console.Write("\r{0}", DateTime.Now);
}
}
@Unity_Scripts
🔸ساختار سوئیچ در سیشارپ
✔️ مثال
using System;
public class Example
{
public static void Main()
{
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
// The example displays the following output:
// Case 1
@Unity_Scripts
✔️ مثال
using System;
public class Example
{
public static void Main()
{
int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
// The example displays the following output:
// Case 1
@Unity_Scripts
🔸حلقه ی تکرار while:
while(condition){
//code to be executed
}
✔️مثال:نمایش اعداد 1 تا 10
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i);
i++;
}
}
}
➖Output:
1
2
3
4
5
6
7
8
9
10
✔️مثال:حلقه ی While تو در تو :
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=3)
{
int j = 1;
while (j <= 3)
{
Console.WriteLine(i+" "+j);
j++;
}
i++;
}
}
}
➖Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
✔️مثال:حلقه ی while بی پایان
(شرط حلقه همیشه درست یا true باشد):
using System;
public class WhileExample
{
public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("Infinitive While Loop");
}
}
}
➖Output:
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
ctrl+c
@Unity_Scripts
while(condition){
//code to be executed
}
✔️مثال:نمایش اعداد 1 تا 10
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i);
i++;
}
}
}
➖Output:
1
2
3
4
5
6
7
8
9
10
✔️مثال:حلقه ی While تو در تو :
using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=3)
{
int j = 1;
while (j <= 3)
{
Console.WriteLine(i+" "+j);
j++;
}
i++;
}
}
}
➖Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
✔️مثال:حلقه ی while بی پایان
(شرط حلقه همیشه درست یا true باشد):
using System;
public class WhileExample
{
public static void Main(string[] args)
{
while(true)
{
Console.WriteLine("Infinitive While Loop");
}
}
}
➖Output:
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
Infinitive While Loop
ctrl+c
@Unity_Scripts
سلام دوستان شما به ما بگید که چه پکیجی رو دوست دارید ما بسازیم برای شما که بخریدش به ما بگید در آی دی زیر
@Erfan_R1380
و تمام این پکیج ها در کانال زیر
@Unity_Package
@Erfan_R1380
و تمام این پکیج ها در کانال زیر
@Unity_Package
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public GameObject[] Boxse;
public Vector3 TranslateSpeed = Vector3.zero;
public float MaxY = 20f;
void Start () {
StartCoroutine ("MoveBoxes");
}
void Updete(){
}
IEnumerator MoveBoxes(){
yield return new WaitForSeconds (2f);
foreach (GameObject Box in Boxse) {
while(Box.transform.position.y < MaxY){
Box.transform.Translate ( TranslateSpeed.x*Time.deltaTime,TranslateSpeed.y*Time.deltaTime,TranslateSpeed.z*Time.deltaTime);
yield return null;
}
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public GameObject[] Boxse;
public Vector3 TranslateSpeed = Vector3.zero;
public float MaxY = 20f;
void Start () {
StartCoroutine ("MoveBoxes");
}
void Updete(){
}
IEnumerator MoveBoxes(){
yield return new WaitForSeconds (2f);
foreach (GameObject Box in Boxse) {
while(Box.transform.position.y < MaxY){
Box.transform.Translate ( TranslateSpeed.x*Time.deltaTime,TranslateSpeed.y*Time.deltaTime,TranslateSpeed.z*Time.deltaTime);
yield return null;
}
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// attach to UI Text component (with the full text already there)
public class UITextTypeWriter : MonoBehaviour {
Text txt;
string story;
void Awake () {
txt = GetComponent<Text> ();
story = txt.text;
txt.text = ""; // TODO: add optional delay when to start
StartCoroutine ("PlayText");
}
IEnumerator PlayText() {
foreach (char c in story) {
txt.text += c;
yield return new WaitForSeconds (0.125f);
}
}
}
using System.Collections;
using UnityEngine.UI;
// attach to UI Text component (with the full text already there)
public class UITextTypeWriter : MonoBehaviour {
Text txt;
string story;
void Awake () {
txt = GetComponent<Text> ();
story = txt.text;
txt.text = ""; // TODO: add optional delay when to start
StartCoroutine ("PlayText");
}
IEnumerator PlayText() {
foreach (char c in story) {
txt.text += c;
yield return new WaitForSeconds (0.125f);
}
}
}
اسکریپت اجرای فیلم در یونیتی👇
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public MovieTexture film1;
void Start() {
renderer.material.mainTexture = film1;
film1.Play();
}
}
@Unity_Scripts
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public MovieTexture film1;
void Start() {
renderer.material.mainTexture = film1;
film1.Play();
}
}
@Unity_Scripts