public override void OnImportAsset(AssetImportContext ctx)
        {
            var inputType = Path.GetExtension(ctx.assetPath);

            if (inputType == null)
            {
                throw new Exception("Demonstration import error.");
            }

            try
            {
                // Read first two proto objects containing metadata and brain parameters.
                Stream reader = File.OpenRead(ctx.assetPath);

                var metaDataProto = DemonstrationMetaProto.Parser.ParseDelimitedFrom(reader);
                var metaData      = new DemonstrationMetaData(metaDataProto);

                reader.Seek(DemonstrationStore.MetaDataBytes + 1, 0);
                var brainParamsProto = BrainParametersProto.Parser.ParseDelimitedFrom(reader);
                var brainParameters  = new BrainParameters(brainParamsProto);

                reader.Close();

                var demonstration = ScriptableObject.CreateInstance <Demonstration>();
                demonstration.Initialize(brainParameters, metaData);
                userData = demonstration.ToString();

                Texture2D texture = (Texture2D)
                                    AssetDatabase.LoadAssetAtPath(IconPath, typeof(Texture2D));

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(ctx.assetPath, demonstration, texture);
                ctx.SetMainObject(demonstration);
#else
                ctx.SetMainAsset(ctx.assetPath, model);
#endif
            }
            catch
            {
                return;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates demonstration file.
        /// </summary>
        private void CreateDemonstrationFile(string demonstrationName)
        {
            // Creates demonstration file.
            var literalName = demonstrationName;

            filePath = DemoDirecory + literalName + ExtensionType;
            var uniqueNameCounter = 0;

            while (fileSystem.File.Exists(filePath))
            {
                literalName = demonstrationName + "_" + uniqueNameCounter;
                filePath    = DemoDirecory + literalName + ExtensionType;
                uniqueNameCounter++;
            }

            writer   = fileSystem.File.Create(filePath);
            metaData = new DemonstrationMetaData {
                demonstrationName = demonstrationName
            };
            var metaProto = metaData.ToProto();

            metaProto.WriteDelimitedTo(writer);
        }
 public void Initialize(BrainParameters brainParameters,
                        DemonstrationMetaData demonstrationMetaData)
 {
     this.brainParameters = brainParameters;
     metaData             = demonstrationMetaData;
 }