Is it possible to develop an application that will offer fields for uploading your own sprites to the user? When loading sprites, they are "attached" to any collider on stage. Then it all moves (maybe with animation, if not, ok too). What is worth reading?
- 2Can. I think it is necessary to study the device API to open a dialog box for selecting a picture. Upload a picture of the resulting path. And then on the prepared sprite you hang up loaded. - Valera Kvip
- I think you should ask for the sample code, and not ask for something to read, since the link responses are prohibited. - Vadim Ovchinnikov
1 answer
There are a couple of ways, but immediately a reservation: works for single sprites. That is, SpriteMode will be Single . To animate sprites you need a minimum of their atlas or mapping, which is quite difficult to do in real time because there are a lot of nuances: how to cut sprites? Indeed, in the spritelist can be located not only on any flat grid, but also randomly, as in the atlas. If you cut a spritelist, then where to put a pivot point for each sprite? How to assign specific sprites from a sheet in SpriteRenderer ? What should be the scale? In general, it will be a nightmare and horror. Therefore, I personally will not consider this option here.
For single sprites:
For the device that the application / game is aimed at, you need to learn / study the methods / methods of opening a dialog box in which you can select a picture and read the path to the file from there. This is an extensive topic, so it does not fit here.
(!!!) In this answer, I will consider trite Windows .
Knowing the file path can be done:
a) Use the WWW class to download content. Also, let's say you have objects under the sprites on the scene in advance and you just need to plug in the sprite.
public SpriteRenderer spriteRenderer; string directory = "file://d:/unityProjects/test/spriteTestLoad.jpg"; void Start () { StartCoroutine(LoadImages()); } private IEnumerator LoadImages() { WWW www = new WWW(directory); // Ожидаем загрузку ресурса yield return www; var tex = www.texture; // Создаем спрайт из текстуры var mySprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); // В подготовленный spriteRenderer вставляем спрайт spriteRenderer.sprite = mySprite; }Note that the directory name starts with
file://. In any case, when downloading a file to Windows usingWWWit is necessary to specify this way. If you download from the Internet, then simply fromhttp, for example:http://images.earthcam.com/ec_metros/ourcams/fridays.jpgHere the path to the file is set rigidly, but you keep in mind that it will just have to be filled in from the dialog box from item No. 1.
b) Using
System.IO;. While also assuming that it is necessary to stick the sprite into the existing space.using UnityEngine; using System.Collections; using System.IO; public class LoadSpriteFromOut : MonoBehaviour { public SpriteRenderer spriteRenderer; string directory = "d:/unityProjects/test/spriteTestLoad.jpg"; void Start () { LoadSprite(); } private void LoadSprite() { byte[] data = File.ReadAllBytes(directory); Texture2D texture = new Texture2D(64, 64, TextureFormat.ARGB32, false); texture.LoadImage(data); texture.name = Path.GetFileNameWithoutExtension(directory); var mySprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f); spriteRenderer.sprite = mySprite; } }c) (converted "a") And we should not forget that you can create objects dynamically, so it is not necessary to prepare them in advance in Unity (here the load from the first option is used):
using UnityEngine; using System.Collections; using System.IO; public class LoadSpriteFromOut : MonoBehaviour { string directory = "file://d:/unityProjects/test/spriteTestLoad.jpg"; void Start () { StartCoroutine(LoadImages()); } private IEnumerator LoadImages() { WWW www = new WWW(directory); yield return www; var texture = www.texture; var mySprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100.0f); // Создаем объект GameObject gameObjectSprite = new GameObject("myNewGameObjectSprite"); // Прикрепляем компонент SpriteRenderer для спрайтов SpriteRenderer gameObjectSpriteSpriteRenderer = gameObjectSprite.AddComponent<SpriteRenderer>(); // втыкаем загруженный спрайт gameObjectSpriteSpriteRenderer.sprite = mySprite; } }
Link to classes and methods: