unity guid что это
What is Unity GUID — How to Get & Change GUID?
Today we’ll figure out what Unity GUID is, and why it can be useful to you when developing games or assets for the Unity Asset Store.
What is Unity GUID?
Unity GUID is a Globally Unique Identifier for the asset.
Scenes, Prefabs, and other Unity files (assets) reference to Asset GUIDs to uniquely identify files within a single project and between different projects (also, with help of code GUID can be used for cross-scene referencing for Game Objects).
How to Get GUID?
GUID is generated by Unity app automatically when you:
Why change Unity GUID?
Under normal conditions, you do not need to change Unity GUID. This is necessary when you are working with several projects and the files in these projects are being overlapped.
For some reason, the same file in different projects can have different GUIDs. In this situation, these are different files for Unity. When these same files (along with related files) with different GUIDs occur in the same (or new) project, there may be problems. In this case, you need to bring the same files to a single GUID. This is relevant when you are dealing with modularity.
Or, on the contrary, you may want to intentionally change the GUIDs for a file or group of files. It is relevant when you are making a new project (or a new file) based on an existing one (with help of copy-paste the folder of the project). At the same time, you change the files in the new project and want to identify them in different ways for further usage.
How to change GUID?
Manual Mode
Example (Generate GUID):
Auto Mode with “GUID Regenerator” tool
If you need to change GUIDs for many files with related files, you can do it with help of “GUID Regenerator” tool.
“GUID Regenerator” is a tool for creating new Asset GUIDs for existing files inside Unity Project. You can find it in a package of Book for Asset Publishers.
Use “Menu > Makaka Games > Regenerate GUIDs for assets in specific folder”.
Cross-scene referencing for Game Objects using GUID
There is opportunity to use next system: special component for giving Game Object a GUID and a class to create references to objects in any Scene by GUID. Get with GitHub.
How to Create Unity Assets?
This article is a part of my book in which I’ve systemized the accumulated knowledge and experience concerning the business in Unity Asset Store.
For Unity Publishers. By Unity Publisher.
Unity guid что это
🆔 Unity GUID Regenerator
A Unity editor tool to regenerate GUID for your assets
⚠️ Disclaimer: Only use this if needed. Intentionally modifying the GUID of an asset is not recommended unless certain files are problematic
What is GUID in Unity?
GUID is a unique hash that is automatically generated and assigned when creating a new asset. This GUID is used when a serialized asset references another asset.
It is stored in an asset’s meta file. Manually you can open and view a meta file in a text editor to get the GUID. However, Unity has its AssetDatabase API that is useful for accessing and performing operations on assets.
Why regenerate a GUID?
When you work on multiple projects that are based on existing projects, chances are most of the assets have the same GUID. This can cause some issues later on when you add more assets from existing projects. Unity won’t be able to distinguish the difference between assets with same GUID even they have different file name and contents. This can cause Unity to associate references to the wrong asset.
The simplest workaround for this is to duplicate the asset. The newly created asset will have its own GUID assigned by Unity. However, you will need to manually replace all its references in the Scene, prefabs, etc.
This project is open source and available under the MIT License.
Assets are tracked by GUID, not by name
The first thing to understand, and this might be a bit surprising at first, is that Unity Editor doesn’t care about the name of the Assets in your project.
The reason it doesn’t care is because it assigns every Asset a Globally Unique Identifier (GUID) and uses that GUID to track the Asset within your project.
If you think about it, that makes a good deal of sense. By using GUIDs, Unity can’t become confused when a developer accidentally gives two Assets the same name.
It also allows Unity to keep track of Assets when you move or rename them. If you have ever tried moving an Asset to a new folder or renaming it in the Editor, you’ll find that all the references to Asset remain intact.
Reorganizing your texture assets into sub-directories? Renaming an Ogre to Troll? The Unity Editor’s got your back, keeping any references intact within the project.
Pro tip: By using the Unity Editor to make changes to Assets, you reduce the odds of them becoming broken. If you are using git for source control, our MetaBuddy plugin for Unity helps you automatically detect any breakages before you commit them.
References between objects are stored as GUIDs
When one object in our project references another, for instance a Material referencing a Texture, the referencer object holds a copy of the referenced object’s GUID.
In the case of a Material referencing a Texture, the Material would hold a copy of Texture’s GUID. The referencer uses the GUID to “point forwards” to the object being referenced. This is illustrated in the diagram below:
They contain two key items of information:
Once we understand this is, it should be clear that meta files aren’t something created by our version control system or an irritating side effect that we can turn off.
Key point: Meta files contain valuable information that isn’t stored elsewhere. Hence, they form an integral part of your project.
This means that they should be checked into your version control system along with the rest of your project.
Unity Assets live in pair of files
Unity creates and maintains a meta file for each Asset in your project. It associates the Asset’s content file with its meta file through its filename.
For instance an Asset with a content file Explosion.png would have a meta file named Explosion.png.meta stored alongside it.
Exactly how the breakage will manifest itself depends on the circumstances. We cover these kind of breakages and how to avoid them here
Altering an Asset’s GUID will break all references to that Asset
The downside to relying on GUIDs to identify Assets is that changing a GUID is a catastrophic event. When a GUID changes, all references to that Asset will become broken.
In many cases, the Unity Editor either remains silent about broken references, or waits until you hit Play before bringing them to your attention.
If you have ever seen the “The referenced script on this Behaviour is missing!” message in Unity’s Console pane, you are seeing the effect of a broken reference to an Asset, a Script in this case.
Pro tip: While Unity is pretty vocal about broken references to Script Assets, it is less vocal about other Asset types.
In many cases, a broken reference will go unnoticed until someone notices that it looks wrong at runtime.
What next?
If you are looking for best practices to avoid common issues with meta files, head over to avoiding broken references
Check out the FAQ for answers to specific Asset and meta file questions. Alternatively, see the Meta File Deep Dive article if you are interested in learning more about what happens under the hood.
Unity guid что это
Globally Unique Identifier
An implementation of a GUID, it can be used standalone, with the GUID component provided, or inside any script which can derive from it. Unlike Unity’s Object.GetInstanceID() this implementation is persistent as it gets serialized inside the component itself.
You can add the GUID component to any gameobject by clicking Add Component > Stone Shelter > Core > GUID
Usage (script inheritance)
You can inherit from the GUID script and add your own functionalities on top, remember to include the correct namespace by adding on top of your file:
using StoneShelter;
Inside your scripts you can retrieve the GUID from a gameobject by using GetComponent ().guid which will give you the current GUID as a string.
You can also retrieve any object from any scene with the static method GUID.Find(guid) where you can pass a guid as a string.
GUID generation and reset
When you add a GUID component (or a component derived from it) for the first time to a gameobject a new GUID is automatically generated:
You can manually generate a new GUID or reset it (for example if you duplicated the gameobject) from the context menu by clicking on the three dots on top of the component and then clicking one of the options on the bottom:
If you are working with prefabs there are some things that you need to know:
About
Implementation of a persistent serializable GUID (Globally Unique Identifier) for Unity.
Unity Meta File FAQ
We wrote this FAQ to answer the most common questions that we get asked about meta files. Hopefully it will help save you time and effort debugging Asset and meta file issues so you can focus on building great software.
While this FAQ is focussed on providing succinct answer to common questions, it forms part of the larger Unity Meta File Guide, which covers the subject in more detail. Many of the answers below include links to more detailed articles where you can dig deeper.
Got a question that we don’t answer?
Send it over and we’ll see what we can do to answer it for you.
What is a Unity meta file?
The Unity Editor creates meta files to track the identity and settings of every Asset in your project.
Meta files are named after the Asset they are associated with. For instance, a texture Asset with the filename Explosion.png would have a meta file named Explosion.png.meta
This article covers Unity’s Asset and meta files in more detail.
What is a Unity GUID?
The Unity Editor uses 32-digit hexadecimal strings called Globally Unique Identifiers (GUIDs) to identify Assets within a project.
Unity uses GUIDs to track references between Assets. If an Asset’s GUID changes, all references to that GUID will become broken.
You can read more about how Unity uses GUIDs to track references here. To learn how to avoid broken references head over to Avoiding Broken References in Unity Projects
Should I add Unity’s meta files to version control?
Meta files are used to store the settings for each Asset along with a Globally Unique Identifier (GUID) that is used to track references between Assets, for instance when a Material Asset references a Texture Asset.
Learn how to version control your Unity project using Git in this how-to guide.
Once you have added your project’s meta files to version control, you need keep them synchronized with the Assets that they relate to. If you are using git, our MetaBuddy plugin for Unity helps you do this every time you commit.
Are Unity meta files just used for version control?
No. Unity meta files contain valuable information about the Assets in your project. You should check them into your version control system (VCS).
You can learn how to setup a Unity project correctly using Git and GitHub in this how-to guide.
No. Unity meta files contain valuable information about the assets in your project. You should add them to your git repository.
Should I choose hidden or visible meta files when using Unity with a Version Control System?
Configure Visible Meta Files in Unity’s Project Settings when using Unity with a version control system like git or Subversion. By making meta files visible, you can add them to your VCS along with the rest of your Unity project.
At the time of writing (February 2019) Unity uses Visible Meta Files by default for newly created projects.
To turn them on for legacy projects chose Edit → Project Settings → Editor from the Unity Editor’s application menu. Then choose Visible Meta Files from the Version Control dropdown.
What Asset Serialization Mode should I choose when using Unity with a Version Control System?
Unity’s Asset Serialization Mode controls how meta files and Scenes are stored on your hard drive. You should set Unity to use the Force Text setting for Asset Serialization Mode when working with a VCS.
In this mode, Unity stores Assets and Scenes in a text-based YAML format. This makes it possible to merge conflicts between differing versions of the file.
At the time of writing (February 2019) Unity uses Force Text as the default for new projects.
To set this mode for legacy projects chose Edit → Project Settings → Editor from the Unity Editor’s application menu. Then choose Force Text from the Asset Serialization Mode dropdown.
How does Unity track references between Assets?
When an Asset (for instance a Material) in your project references an another Asset that it depends on (for instance a Texture) it points that asset by GUID. ie. The Material stores a copy of the Texture’s GUID.
The crucial thing to recognise here is that the name of Asset is immaterial to the Unity Editor, it only cares about the GUID for referencing purposes. You can find more details in this guide to avoiding broken references
How do avoid I broken references and missing meta files in Unity?
Modifying Assets outside Unity Editor is the more common cause of broken references and missing meta files.
The easiest way to avoid this is to always create, move and rename your Assets using the Unity Editor.
When you do this, the Editor manages references and meta files behind the scenes on your behalf, avoiding breakages.
Another common cause is deleting and re-adding Assets in the Editor in order to modify their contents.
See Avoiding Broken References to learn the common causes of Unity project breakage and how to avoid them.
Why do meta files keep appearing unexpectedly in my Unity project?
When the Editor detects a new file in the Assets folder that doesn’t have a companion meta file, it attempts to that file into your project as a new Asset. This creates a meta file to hold the Globally Unique Identifier (GUID) and settings for the newly added Asset.
For instance, if you copy an image file Explosion.png to the Assets folder, the Editor will import it as a texture, creating a meta file Explosion.png.meta alongside it.
A common cause of mystery meta files is when developer forgets to include a meta file when adding a new asset to version control. This often means that any references to the new asset will be silently broken.
You can use a tool like MetaBuddy or a pre-commit checklist to avoid this common problem.
What causes ‘The referenced script (Unknown) on this Behaviour is missing!’ error message in Unity?
The Unity Editor shows this message when you enter Play mode in a project containing a broken reference to a Script Asset.
This usually means that the Script Asset has been deleted or its GUID has been inadvertently changed.
See our avoiding broken references article to learn how broken references can be avoided in Unity projects.
The Unity Editor shows this message when you enter Play mode in a project containing a broken reference to a Script Asset.
This usually means that the Script Asset has been deleted or its GUID has been inadvertently changed.
Clicking on this error message in the Console pane will highlight the GameObject holding the broken reference in the Hierarchy pane.
Head over to Avoiding Broken References to learn how to avoid the broken references that cause this error message.