✅✅✅ذخیره مختصات یک باتن در یونیتی_تغییر مختصات button _دستری به position یک button در یونیتی:
@Unity_Scripts
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);
@Unity_Scripts
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);
✅✅✅ذخیره مختصات یک باتن در یونیتی_تغییر مختصات button _دستری به position یک button در یونیتی:
@Unity_Scripts
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);
@Unity_Scripts
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);
✅✅🔫🔫👍😊 کد شلیک گلوله سه بعدی و نابود شدن دشمن پس از برخورد تیر با هدف که تگ enemy دارد.
کد تیر اندازی در صورتی که خشاب،تیر داشته باشه برای یونیتی.کد تیر زدن در یونیتی_کد تیر اندازی در یونیتی_کد شلیک تیر در یونیتی_کد شلیک گلوله در یونیتی_اسکریپت شلیک در یونیتی_اسکریپت تیر اندازی در یونیتی_اسکریپت تیر زدن در یونیتی💛👻
// @Unity_Scripts
using UnityEngine;
using System.Collections;
public class tirZadan: MonoBehaviour {
// Use this for initialization
public Transform emptyGameObject;
public float distance;
public GameObject enemy,prefBullet,bullet;
public AudioClip shootAudio;
public Transform shootPoint;
float volumeSeda=1.0F;
int tedadTir=10;
void Update () {
if (Input.GetButtonDown ("Fire1")) //age left_click shod
{
if(tedadTir > 0) {
tedadTir--;
bullet= Instantiate(prefBullet, emptyGameObject.position, Quaternion.identity)as GameObject;
AudioSource.PlayClipAtPoint(shootAudio, shootPoint.transform.position, volumeSeda);
}
}
}//update
}
✅👻 کد زیر هم به پریفب گلوله میدیم:
//bullet.cs
using UnityEngine;
using System.Collections;
public class bullet: MonoBehaviour {
public RigidBoy rb;
float distance=0.0f;//fasele sefr yani faghat vaghti tir barkhord kard be hadaf
void Start(){
rb.AddRelativeForce(Vector3.forward );//partabe tir be samte jelo
Destroy(gameObject, 3.0f);//hazfe tir pas az 3 sanie
}
void OnGUI(){
myRaycast();
}
void myRaycast(){
RaycastHit hit;
Ray myRay = new Ray(transform.position, Vector3.forward);
if(Physics.Raycast (myRay, out hit, distance)){
if(hit.collider.tag == "enemy"){
Destroy(enemy);
Destroy(this.gameobject);
}//if
}//if
}//myRaycast
}
💛 نکته 💛
تیر و دشمن هر دو پس از برخورد از بین میرن.
تیر در صورت عدم برخورد هم پس از سه ثانیه از بین میره.
شما باید پریفب تیر رو درگ کنید روی متغیر prefBullet.
(برای ساخت پریفب،کافیه یک مدل از تیر رو از داخل hierarchy درگ کنید به کادر project. سپس تیر رو از داخل صحنه حذف کنید)
کد تیر اندازی در صورتی که خشاب،تیر داشته باشه برای یونیتی.کد تیر زدن در یونیتی_کد تیر اندازی در یونیتی_کد شلیک تیر در یونیتی_کد شلیک گلوله در یونیتی_اسکریپت شلیک در یونیتی_اسکریپت تیر اندازی در یونیتی_اسکریپت تیر زدن در یونیتی💛👻
// @Unity_Scripts
using UnityEngine;
using System.Collections;
public class tirZadan: MonoBehaviour {
// Use this for initialization
public Transform emptyGameObject;
public float distance;
public GameObject enemy,prefBullet,bullet;
public AudioClip shootAudio;
public Transform shootPoint;
float volumeSeda=1.0F;
int tedadTir=10;
void Update () {
if (Input.GetButtonDown ("Fire1")) //age left_click shod
{
if(tedadTir > 0) {
tedadTir--;
bullet= Instantiate(prefBullet, emptyGameObject.position, Quaternion.identity)as GameObject;
AudioSource.PlayClipAtPoint(shootAudio, shootPoint.transform.position, volumeSeda);
}
}
}//update
}
✅👻 کد زیر هم به پریفب گلوله میدیم:
//bullet.cs
using UnityEngine;
using System.Collections;
public class bullet: MonoBehaviour {
public RigidBoy rb;
float distance=0.0f;//fasele sefr yani faghat vaghti tir barkhord kard be hadaf
void Start(){
rb.AddRelativeForce(Vector3.forward );//partabe tir be samte jelo
Destroy(gameObject, 3.0f);//hazfe tir pas az 3 sanie
}
void OnGUI(){
myRaycast();
}
void myRaycast(){
RaycastHit hit;
Ray myRay = new Ray(transform.position, Vector3.forward);
if(Physics.Raycast (myRay, out hit, distance)){
if(hit.collider.tag == "enemy"){
Destroy(enemy);
Destroy(this.gameobject);
}//if
}//if
}//myRaycast
}
💛 نکته 💛
تیر و دشمن هر دو پس از برخورد از بین میرن.
تیر در صورت عدم برخورد هم پس از سه ثانیه از بین میره.
شما باید پریفب تیر رو درگ کنید روی متغیر prefBullet.
(برای ساخت پریفب،کافیه یک مدل از تیر رو از داخل hierarchy درگ کنید به کادر project. سپس تیر رو از داخل صحنه حذف کنید)
تیر زدن به گیم ابجکتی که وارد یک محدوده شده:
چاپ اسم دشمنی که به محدوده ی ما وارد شده:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnTriggerStay(Collider other) {
کد تیر زدن به سویش
}
}
@Unity_Scripts
چاپ اسم دشمنی که به محدوده ی ما وارد شده:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnTriggerStay(Collider other) {
کد تیر زدن به سویش
}
}
@Unity_Scripts
🏅ایجاد پریفب تیر با صدا
bullet= Instantiate(prefBullet, emptyGameObject.position, Quaternion.identity)as GameObject;
AudioSource.PlayClipAtPoint(shootAudio, shootPoint.transform.position, volumeSeda);
bullet= Instantiate(prefBullet, emptyGameObject.position, Quaternion.identity)as GameObject;
AudioSource.PlayClipAtPoint(shootAudio, shootPoint.transform.position, volumeSeda);
با این کد، می توانیم یک دکمه بسازیم که برای انتخاب همه گزینه ها یا عدم انتخاب همه گزینه ها به کار رود. کاربر می تواند یک یا چند گزینه را انتخاب کند، ولی زمانی که قصد دارد که همه گزینه ها انتخاب شوند و یا از حالت انتخاب در بیایند، می تواند از این دکمه استفاده کند. نمونه آن در زیر نمایش داده شده است :
کدش:☝👇
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
</script>
</head>
<body>
<center>
<form name="myform" action="" method="post">
<table>
<tr><td>
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">Java<br>
<input type="checkbox" name="list" value="2">JavaScript<br>
<input type="checkbox" name="list" value="3">ASP<br>
<input type="checkbox" name="list" value="4">HTML<br>
<input type="checkbox" name="list" value="5">SQL<br>
<br>
<input type="button" value="Check All" onClick="this.value=check(this.form.list)">
</td></tr>
</table>
</form>
</center>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;}
checkflag = "true";
return "Uncheck All"; }
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false; }
checkflag = "false";
return "Check All"; }
}
</script>
</head>
<body>
<center>
<form name="myform" action="" method="post">
<table>
<tr><td>
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="1">Java<br>
<input type="checkbox" name="list" value="2">JavaScript<br>
<input type="checkbox" name="list" value="3">ASP<br>
<input type="checkbox" name="list" value="4">HTML<br>
<input type="checkbox" name="list" value="5">SQL<br>
<br>
<input type="button" value="Check All" onClick="this.value=check(this.form.list)">
</td></tr>
</table>
</form>
</center>
</body>
</html>
باز کردن صفحه ی وب/باز کردن صفحه وب در یونیتی/باز کردن سایت در یونیتی/باز کردن url در یونیتی/باز کردن لینک در یونیتی:
Application.OpenURL("http://www.google.com");
Application.OpenURL("http://www.google.com");
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;
}
}
}
@Unity_Scripts
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;
}
}
}
@Unity_Scripts
کد بهتر یونیتی برای لود کردن سین ها بدون تیک زدن بازی:(از یونیتی 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);
}
}
}