public IEnumerator GetRemoteTexture(string url, Action<Texture2D> response) {
UnityWebRequest request = UnityWebRequestTexture.GetTexture(url);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success){
response(null);
} else {
response(DownloadHandlerTexture.GetContent(request));
}
}
using UnityEngine;
public class MusicDemo : MonoBehaviour {
public AudioSource audio; // в инспекторе сюда перетащите источник звука
public void OnPressed() { // привяжите к кнопке этот метод
if (audio.isPlaying) // если аудио играет
audio.Pause(); // поставить на паузу
else // иначе
audio.UnPause(); // снять с паузы
}
}
// ...
using System.Collections.Concurrent;
// ...
private readonly ConcurrentQueue<Action> _Actions = new ConcurrentQueue<Action>();
// ...
// Когда вам нужно что-то запустить из другого потока, вызываете тут
_Actions.Enqueue(() => {
// Ваш код
});
// ...
private void Update() {
while (_Actions.Count > 0)
if (_Actions.TryDequeue(out var action))
action?.Invoke();
// ...
}
using UnityEngine;
public class SimpleTrigger : MonoBehaviour {
public GameObject button;
private void OnTriggerEnter2D(Collider2D c) {
if (c.gameObject.tag == "Player")
button.SetActive(true);
}
}
if (Physics.Raycast(transform.position, _direction, out hit, 10f)) {
if (hit.collider == yourCollider) {
// some logic
}
}
private MyComponent GetTarget() {
if (Physics.Raycast(transform.position, _direction, out hit, 10f))
if (hit.transform.TryGetComponent(out MyComponent m))
return m;
return null;
}
using UnityEngine;
public class PlatformMove : MonoBehaviour {
// movement
private void OnCollisionEnter2D(Collision2D c) {
if (c.gameObject.tag == "Player")
c.transform.parent = transform;
}
private void OnCollisionExit2D(Collision2D c) {
if (c.gameObject.tag == "Player")
c.transform.parent = null;
}
}
// imageTitle - имя файла, например, time stamp
string pathToFile = Application.persistentDataPath + "/" + imageTitle + ".png";
using System.IO;
// ...
public bool SavePNG(Texture2D texture, string pathToFile) {
if (!File.Exists(pathToFile)) {
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(pathToFile, bytes);
return true;
}
return false;
}
public static Texture2D LoadPNG(string pathToFile) {
Texture2D texture = null;
if (File.Exists(pathToFile)) {
byte[] fileData = File.ReadAllBytes(pathToFile);
texture = new Texture2D(2, 2);
texture.LoadImage(fileData);
}
return texture;
}
using UnityEngine;
public class MouseCenteringExample : MonoBehaviour {
private void Start() {
Cursor.lockState = CursorLockMode.Locked;
Invoke("UnlockCursor", 0.05f);
}
private void UnlockCursor() => Cursor.lockState = CursorLockMode.None;
}
using UnityEngine;
public class MouseCenteringExample : MonoBehaviour {
private void Start() {
var center = Screen.safeArea.center;
Mouse.current.WarpCursorPosition(center);
InputState.Change(Mouse.current.position, center);
}
}
using System.Runtime.InteropServices;
using UnityEngine;
public class MouseCenteringExample : MonoBehaviour {
#if UNITY_STANDALONE_WIN
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
private void Start() {
var center = Screen.safeArea.center;
SetCursorPos((int)center.x, (int)center.y);
}
#endif
}
using TMPro;
using UnityEditor;
using UnityEngine;
public class Calc : MonoBehaviour {
[SerializeField] private TMP_InputField m_inputField;
[SerializeField] private TextMeshProUGUI m_text;
public void OnCalculate() {
ExpressionEvaluator.Evaluate(m_inputField.text, out int result);
m_text.text = result.ToString();
}
}